Swift开发之 ---- Swift宏定义

swift中没有了#Define这种宏定义了,可以用let来声明常量来取代,判断当前系统版本

let IS_IOS7 = (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 7.0 let IS_IOS8 = (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 8.0

 PS:这种用let替代#define只适用于一般的常量宏,如果是表达式或者其他复杂的宏,let也无能无力

对于复杂表达式的宏,可以用全局的func函数代替,比如上面的两个系统判断,可以修改成下面的func

func IS_IOS7() ->Bool { return (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 7.0 } func IS_IOS8() -> Bool { return (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 8.0 }

这样,就可以在三元表达式中使用了:

navBar = UIView(frame: CGRectMake(0, 0, 320, IS_IOS7() ? 64:44))

还有RGBA宏

RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]

可以写成这样:

func RGBA (r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) { return UIColor (red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a) }
var theColor : UIColor = RGBA (255, 255, 0, 1)
原文地址:https://www.cnblogs.com/isItOk/p/4939241.html