Swift本身不支持宏定义,因此相关的操作采用的是常量代替
1.创建一个Swift文件专门用于存放代替宏的常量
2.常量->宏定义
let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.height
let SCREEN_WIDTH = UIScreen.mainScreen().bounds.width
let FIT_WIDTH = UIScreen.mainScreen().bounds.size.width / 375
let FIT_HEIGHT = UIScreen.mainScreen().bounds.size.height / 667
let FONT_30 = UIFont.systemFontOfSize(15)
let FONT_60 = UIFont.systemFontOfSize(30)
let FONT_140 = UIFont.systemFontOfSize(70)
let systemVersion = (UIDevice .currentDevice().systemVersion as String)
3.全局函数->宏函数
func x(object: UIView) ->CGFloat {
return object.frame.origin.x
}
func y(object: UIView) -> CGFloat {
return object.frame.origin.y
}
func width(object: UIView) -> CGFloat {
return object.frame.size.width
}
func height(object: UIView) -> CGFloat {
return object.frame.size.height
}
func centerX(object: UIView) -> CGFloat {
return object.center.x
}
func centerY(object: UIView) -> CGFloat {
return object.center.y
}
//RGB
func RGB(r:CGFloat,g:CGFloat,b:CGFloat)->UIColor{
return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
}
由于是全局的,使用起来直接调用即可
此时你可能会对宏定义提出疑问,如果有宏定义来进行编译控制,#ifdef之类的处理如何做,swift在这点上做了处理,但是需要借借助于编译符号
1.设置编译符号
2.code
#if DEBUGXXX
print("debug xxxx")
#else
print("no debug xxxx")
#endif
3.效果
总结
Swift本身不支持宏,但是我们可以通过常量+编译符号实现一样的效果