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

后端 未结 3 1226
后悔当初
后悔当初 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: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()

提交回复
热议问题