ios 如何在 Swift 中使用带有#define 宏的 Objective-C 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24133695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Objective-C code with #define macros in Swift
提问by ankushg
I'm trying to use a third-party Objective-C library in a Swift project of mine. I have the library successfully imported into Xcode, and I've made a <Project>-Bridging-Header.h
file that's allowing me to use my Objective-C classes in Swift.
我正在尝试在我的 Swift 项目中使用第三方 Objective-C 库。我已将该库成功导入 Xcode,并制作了一个<Project>-Bridging-Header.h
文件,该文件允许我在 Swift 中使用我的 Objective-C 类。
I seem to be running into one issue however: the Objective-C code includes a Constants.h
file with the macro #define AD_SIZE CGSizeMake(320, 50)
. Importing Constants.h
into my <Project>-Bridging-Header.h
doesn't result in a global constant AD_SIZE
that my Swift app can use.
然而,我似乎遇到了一个问题:Objective-C 代码包含一个Constants.h
带有宏#define AD_SIZE CGSizeMake(320, 50)
. 导入Constants.h
my<Project>-Bridging-Header.h
不会导致AD_SIZE
我的 Swift 应用程序可以使用全局常量。
I did some research and saw that the Apple documentation here under "Complex Macros"says that
我做了一些研究,看到“复杂宏”下的 Apple 文档说
“In Swift, you can use functions and generics to achieve the same results [as complex macros] without any compromises. Therefore, the complex macros that are in C and Objective-C source files are not made available to your Swift code.”
“在 Swift 中,您可以使用函数和泛型来实现与复杂宏相同的结果,而不会做出任何妥协。因此,您的 Swift 代码无法使用 C 和 Objective-C 源文件中的复杂宏。”
After reading that, I got it to work fine by specifying let AD_SIZE = CGSizeMake(320, 50)
in Swift, but I want to maintain future compatibility with the library in the event that these values change without me knowing.
读完之后,我通过let AD_SIZE = CGSizeMake(320, 50)
在 Swift 中指定让它正常工作,但我想保持与库的未来兼容性,以防这些值在我不知道的情况下发生变化。
Is there an easy fix for this in Swift or my bridging header? If not, is there a way to replace the #define AD_SIZE CGSizeMake(320, 50)
in Constants.h
and keep things backwards-compatible with any existing Objective-C apps that use the old AD_SIZE
macro?
在 Swift 或我的桥接头中是否有一个简单的解决方法?如果没有,有没有办法替换#define AD_SIZE CGSizeMake(320, 50)
inConstants.h
并保持与使用旧AD_SIZE
宏的任何现有 Objective-C 应用程序向后兼容?
采纳答案by ankushg
I resolved this by replacing
我通过替换解决了这个问题
#define AD_SIZE CGSizeMake(320, 50)
#define AD_SIZE CGSizeMake(320, 50)
in the library's Constants.h
with
在图书馆的Constants.h
与
extern CGSize const AD_SIZE;
extern CGSize const AD_SIZE;
and adding
并添加
CGSize const AD_SIZE = { .width = 320.0f, .height = 50.0f };
CGSize const AD_SIZE = { .width = 320.0f, .height = 50.0f };
in the library's Constants.m
file.
在图书馆的Constants.m
文件中。
回答by YogevSitton
What I did is to create a class method that returns the #define.
我所做的是创建一个返回#define 的类方法。
Example:
例子:
.h file:
.h 文件:
#define AD_SIZE CGSizeMake(320, 50)
+ (CGSize)adSize;
.m file:
.m 文件:
+ (CGSize)adSize { return AD_SIZE; }
And in Swift:
在 Swift 中:
Since this is a class method you can now use it almost as you would the #define. If you change your #define macro - it will be reflected in the new method you created In Swift:
由于这是一个类方法,您现在几乎可以像使用#define 一样使用它。如果您更改 #define 宏 - 它会反映在您在 Swift 中创建的新方法中:
let size = YourClass.adSize()
让大小 = YourClass.adSize()
回答by Mohit Tomar
write your constants after Class declaration. like this...
在类声明之后写你的常量。像这样...
class ForgotPasswrdViewController: UIViewController {
let IS_IPHONE5 = fabs(UIScreen.mainScreen().bounds.size.height-568) < 1;
let Tag_iamTxtf = 101