Swift - using #define in Swift

I have one Common.h

with the following definition:

// Get UIColor from Hex value
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define CellTextColor UIColorFromRGB((uint32_t) 0x217EB5)

      

I want to use CellTextColor

for coloring in Swift. But this prevents me from running macros. Only when I have #define value @"string"

it does it work in Swift.

Is there something else I need to do here?

I have read the following links:

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

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html

+3


source to share


1 answer


The concrete answer is probably a UIColor extension:

extension UIColor {

    class func fromRGB(rgb:UInt32) -> UIColor {
        return UIColor(
            red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgb & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }

    class var cellTextColor: UIColor {
        return UIColor.fromRGB(0x217eb5)
    }

}

      

What allows you to use:



let color = UIColor.cellTextColor

      

The more general answer is that all of this will greatly depend on what the macro is and how it is used.

+3


source







All Articles