iOS常用宏定义(Siwft篇)

swift中并没有宏定义,只是用全局常量和利用函数获取全局常量的方式实现了OC中的宏定义功能。

屏幕

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

最好的办法是使用结构体:

struct ScreenSize {
static let height = UIScreen.mainScreen().bounds.height
static let width = UIScreen.mainScreen().bounds.width
}

系统

//系统
struct System {
static let version = UIDevice .currentDevice().systemVersion
}

Log

/// Log
func printLog<T>(message: T,
file: String = #file,
method: String = #function,
line: Int = #line)
{
#if DEBUG
print("Log:\((file as NSString).lastPathComponent)[\(line)], \(method): \(message)")
#endif
}
/// Error
func printError<T>(message: T,
file: String = #file,
method: String = #function,
line: Int = #line)
{
#if DEBUG
print("Error:\((file as NSString).lastPathComponent)[\(line)], \(method): \(message)")
#endif
}

UIView

使用Extension

// MARK: - UIView 扩展
extension UIView{
var x:CGFloat{
return frame.origin.x
}
var y:CGFloat{
return frame.origin.y
}
var width:CGFloat{
return frame.size.width
}
var height:CGFloat{
return frame.size.height
}
}

设备

public enum DeviceModel : String {
case Simulator = "Simulator/Sandbox",
iPod1 = "iPod 1",
iPod2 = "iPod 2",
iPod3 = "iPod 3",
iPod4 = "iPod 4",
iPod5 = "iPod 5",
iPad2 = "iPad 2",
iPad3 = "iPad 3",
iPad4 = "iPad 4",
iPhone4 = "iPhone 4",
iPhone4S = "iPhone 4S",
iPhone5 = "iPhone 5",
iPhone5S = "iPhone 5S",
iPhone5C = "iPhone 5C",
iPadMini1 = "iPad Mini 1",
iPadMini2 = "iPad Mini 2",
iPadMini3 = "iPad Mini 3",
iPadAir1 = "iPad Air 1",
iPadAir2 = "iPad Air 2",
iPhone6 = "iPhone 6",
iPhone6plus = "iPhone 6 Plus",
iPhone6S = "iPhone 6S",
iPhone6Splus = "iPhone 6S Plus",
iPhone7 = "iPhone 7",
iPhone7plus = "iPhone 7 Plus",
Unrecognized = "unrecognized"
}
extension UIDevice{
/// 获取当前设备详细类型
public var deviceModel: DeviceModel{
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8 , value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
var modelMap : [ String : DeviceModel ] = [
"i386" : .Simulator,
"x86_64" : .Simulator,
"iPod1,1" : .iPod1,
"iPod2,1" : .iPod2,
"iPod3,1" : .iPod3,
"iPod4,1" : .iPod4,
"iPod5,1" : .iPod5,
"iPad2,1" : .iPad2,
"iPad2,2" : .iPad2,
"iPad2,3" : .iPad2,
"iPad2,4" : .iPad2,
"iPad2,5" : .iPadMini1,
"iPad2,6" : .iPadMini1,
"iPad2,7" : .iPadMini1,
"iPhone3,1" : .iPhone4,
"iPhone3,2" : .iPhone4,
"iPhone3,3" : .iPhone4,
"iPhone4,1" : .iPhone4S,
"iPhone5,1" : .iPhone5,
"iPhone5,2" : .iPhone5,
"iPhone5,3" : .iPhone5C,
"iPhone5,4" : .iPhone5C,
"iPad3,1" : .iPad3,
"iPad3,2" : .iPad3,
"iPad3,3" : .iPad3,
"iPad3,4" : .iPad4,
"iPad3,5" : .iPad4,
"iPad3,6" : .iPad4,
"iPhone6,1" : .iPhone5S,
"iPhone6,2" : .iPhone5S,
"iPad4,1" : .iPadAir1,
"iPad4,2" : .iPadAir2,
"iPad4,4" : .iPadMini2,
"iPad4,5" : .iPadMini2,
"iPad4,6" : .iPadMini2,
"iPad4,7" : .iPadMini3,
"iPad4,8" : .iPadMini3,
"iPad4,9" : .iPadMini3,
"iPhone7,1" : .iPhone6plus,
"iPhone7,2" : .iPhone6,
"iPhone8,1" : .iPhone6S,
"iPhone8,2" : .iPhone6Splus,
"iPhone9,1" : .iPhone7,
"iPhone9,2" : .iPhone7plus
]
if let model = modelMap[identifier] {
return model
}
return DeviceModel.Unrecognized
}
/// 判断是不是iPhone
var isiPhone:Bool{
return model == "iPhone"
}
/// 判断是不是iPad
var isiPad:Bool{
return model == "iPad"
}
}