Swift + macro parameters

后端 未结 3 1109
时光取名叫无心
时光取名叫无心 2021-01-06 19:42

I read all Q&A related to Macros in Swift, And i did figure out that everything in Swift now global, Am i right?

3条回答
  •  时光取名叫无心
    2021-01-06 20:14

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

    Example:

    .h file:

    #define COLOR_CODE(red, green, blue, alpha) [UIColor colorWithRed: red/255.0 green: green/255.0 blue: blue/255.0 alpha: alpha]
    + (UIColor*)colorCodeWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
    

    .m file:

    + (UIColor*)colorCodeWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha { return COLOR_CODE(red, green, blue, alpha); }
    

    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 color = YourClass.colorCodeWithRed(72.0, green: 55.0, blue: 100.0, alpha: 1.0);

提交回复
热议问题