Varun Naharia
Varun Naharia

Reputation: 5388

How to define constant in swift?

I searched a lot over the internet to find the right solution to convert my constant .h file to swift. I already tried these links

  1. how to use a objective-c define from swift
  2. Globalconstants file in swift

These both are use full but not completely solved my problem.

I just want to convert these three #define to swift all other can be converted in the same way as these will do.

  1. #define IS_IPHONE_4S [[UIScreen mainScreen] bounds].size.height == 480
  2. #define IOS7VERSION ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?YES:NO)
  3. #define RGBCOLOR(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]

Upvotes: 3

Views: 3748

Answers (1)

Sviatoslav Yakymiv
Sviatoslav Yakymiv

Reputation: 7935

You can use the keyword let for this. For example:

let IsIPhone4S = UIScreen.mainScreen().bounds.height == 480
let IOS7Version = Float(UIDevice.currentDevice().systemVersion) >= 7

And for the last case you should use function:

func RGBColor(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
    return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 1)
}

Upvotes: 3

Related Questions