Swift中没有宏定义,那么全局常量可以建立一个空的Swift文件去统一声明。
// 屏幕的物理宽度
let kScreenWidth = UIScreen.mainScreen().bounds.size.width
// 屏幕的物理高度
let kScreenHeight = UIScreen.mainScreen().bounds.size.height
// navigationBar高度
let kNaviBarHeight: CGFloat = 64
// tabbar高度
let kTabBarHeight: CGFloat = 49
复杂的宏在Swift中则使用全局函数代替
- RGBA的颜色设置
func kRGBA (r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) -> UIColor {
return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}
- App路径
// 沙盒路径
func kAppPath() -> String! {
return NSHomeDirectory()
}
// Documents路径
func kBundleDocumentPath() -> String! {
return NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
}
// Caches路径
func kCachesPath() -> String! {
return NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).first as! String
}
- 打印日志
func printLog<T>(message: T,
file: String = #file,
method: String = #function,
line: Int = #line)
{
#if DEBUG
print("\((file as NSString).lastPathComponent)[\(line)], \(method): \(message)")
#endif
}