swift中没有宏定义了,但是OC当中宏的习惯一时还改不了,想实现类似效果
xcode → File → New → File → Cocoa Touch Class → Next
把uikit 下边的 删掉,然后就可以写了(其实是声明的全局变量)
import UIKit
let ScreenWidth = UIScreen.main.bounds.size.width
let ScreenHeight = UIScreen.main.bounds.size.height
// rgb颜色转换
func UIColorFromRGB (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
if (cString.hasPrefix("#")) {
cString = (cString as NSString).substring(from: 1)
}
let rString = (cString as NSString).substring(to: 2)
let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
Scanner(string: rString).scanHexInt32(&r)
Scanner(string: gString).scanHexInt32(&g)
Scanner(string: bString).scanHexInt32(&b)
return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
}
编译一下,就可以使用了 ,weakself 好像没办法像OC一样宏定了 只能在用的时候,这样写
weak var weakSelf = self
var view = UIView.init(frame: CGRect(x:0,y:0,width:ScreenWidth,height:ScreenHeight))
self.view.backgroundColor=UIColorFromRGB(hex: "0Xe0e6ea")
参考链接: http://www.imooc.com/qadetail/68779