How can I use UIColorFromRGB in Swift?

后端 未结 20 836
一生所求
一生所求 2020-12-04 05:48

In Objective-C, we use this code to set RGB color codes for views:

#define UIColorFromRGB(rgbValue)        
[UIColor colorWithRed:((float)((rgbValue & 0x         


        
20条回答
  •  孤街浪徒
    2020-12-04 06:15

    You cannot use a complex macros like #define UIColorFromRGB(rgbValue) in swift. The replacement of simple macro in swift is global constants like

    let FADE_ANIMATION_DURATION = 0.35
    

    Still the complex macros that accept parameters are not supported by swift. you could use functions instead

    Complex macros are used in C and Objective-C but have no counterpart in Swift. Complex macros are macros that do not define constants, including parenthesized, function-like macros. You use complex macros in C and Objective-C to avoid type-checking constraints or to avoid retyping large amounts of boilerplate code. However, macros can make debugging and refactoring difficult. In Swift, you can use functions and generics to achieve the same results without any compromises. Therefore, the complex macros that are in C and Objective-C source files are not made available to your Swift code.

    Excerpt from Using swift with cocoa and objective C

    Check @Nate Cooks answer for the Swift version of that function to be used here

提交回复
热议问题