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中,并创建了一个
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代码包含一个常量。h文件与宏#定义AD_SIZE CGSizeMake(320, 50)。进口常数。h <项目> -Bridging-Header。h不会产生我的Swift应用可以使用的全局常量AD_SIZE。
I did some research and saw that the Apple documentation here under "Complex Macros" says that
我做了一些研究,发现这里的“复杂宏”下的苹果文档说明了这一点
“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中,您可以使用函数和泛型实现与复杂宏相同的结果,而不需要任何妥协。因此,在C和Objective-C源文件中的复杂宏不能用于您的Swift代码。
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.
在阅读之后,我通过在Swift中指定let AD_SIZE = CGSizeMake(320,50)使它工作得很好,但是我希望在这些值不知情的情况下与库保持未来的兼容性。
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)中的常量。h并保持东西向后兼容任何现有的Objective-C应用使用旧的AD_SIZE宏?
3 个解决方案
#1
10
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:
在迅速:
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()
#2
5
I resolved this by replacing
我通过替换来解决这个问题
#define AD_SIZE CGSizeMake(320, 50)
#定义AD_SIZE CGSizeMake(320年,50)
in the library's Constants.h
with
在图书馆的常数。h和
extern CGSize const AD_SIZE;
走读生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.
在图书馆的常数。m文件。
#3
4
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
#1
10
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:
在迅速:
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()
#2
5
I resolved this by replacing
我通过替换来解决这个问题
#define AD_SIZE CGSizeMake(320, 50)
#定义AD_SIZE CGSizeMake(320年,50)
in the library's Constants.h
with
在图书馆的常数。h和
extern CGSize const AD_SIZE;
走读生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.
在图书馆的常数。m文件。
#3
4
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