您的位置:首页 > 移动开发 > Swift

swift 的宏定义

2015-07-07 11:28 183 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: