加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift 的宏定义

发布时间:2020-12-14 02:03:07 所属栏目:百科 来源:网络整理
导读:swift中没有了#Define这种宏定义了,可以用let来声明常量来取代,判断当前系统版本 let IS_IOS7 = (UIDevice.currentDevice().systemVersion as NSString).doubleValue = 7.0 let IS_IOS8 = (UIDevice.currentDevice().systemVersion as NSString).doubleValu

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

let IS_IOS7 = (UIDevice.currentDevice().systemVersionasNSString).doubleValue >= 7.0 let IS_IOS8 =(UIDevice.currentDevice().systemVersionasNSString).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,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,1)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读