How to use Objective-C code with #define macros in Swift

后端 未结 3 1202
后悔当初
后悔当初 2020-12-03 10:00

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 -

相关标签:
3条回答
  • 2020-12-03 10:34

    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
    
    0 讨论(0)
  • 2020-12-03 10:39

    What I did is to create a class method that returns the #define.

    Example:

    .h file:

    #define AD_SIZE CGSizeMake(320, 50)
    + (CGSize)adSize;
    

    .m file:

    + (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:

    let size = YourClass.adSize()

    0 讨论(0)
  • 2020-12-03 10:49

    I resolved this by replacing

    #define AD_SIZE CGSizeMake(320, 50)

    in the library's Constants.h with

    extern CGSize const AD_SIZE;

    and adding

    CGSize const AD_SIZE = { .width = 320.0f, .height = 50.0f };

    in the library's Constants.m file.

    0 讨论(0)
提交回复
热议问题