3

I have a Common.h with the following defined:

// 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 be able to use CellTextColor for the coloring in Swift. But it's not allowing me to run the macros. Only when I have #define value @"string" it works in Swift.

Is there something else I need to do here?

I read the below 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

Community
  • 1
  • 1
KVISH
  • 12,923
  • 17
  • 86
  • 162

1 Answers1

3

The specific answer is probably an extension on UIColor:

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)
    }

}

Which allows you to use:

let color = UIColor.cellTextColor

The more generic answer is that it's all going to be very dependent on what the macro is and how it winds up being used.

David Berry
  • 40,941
  • 12
  • 84
  • 95