3、Swift-“宏定义”的实现
2020-03-02 本文已影响0人
爱玩游戏的iOS菜鸟
OC中的宏定义
- C语言的宏定义, 并不是很严谨, 任何代码段都可以进行宏定义, 甚至是无效的无序的乱码也可以.
- 因为C语言的机制下, 是在编译代码时将宏直接替换成定义的代码, 在实际使用中是存有安全隐患的
- 一般的宏定义函数或者是计算时, 要在外面加括号, 防止一些错误的出现
#define kFit_6W(x) ([UIScreen mainScreen].bounds.size.width * ((x)/375.0))
#define kFit_6H(x) ([UIScreen mainScreen].bounds.size.height * ((x)/667.0))
Swift"宏定义"
- 设置全局常量, 简单宏, 直接let 加常量名即可,
- 复杂的宏由于必须保证宏的代码的语句的合法性,使用函数进行实现
Swift的宏定义在本质上并不应该称之为宏定义, 只是为了方便大家的理解, 实质上是一些全局常量和函数
下面是我根据平常OC使用到的宏定义进行一定的改写,有些全局函数或常量并无必要,仅仅做学习用,并非代表写法更好,希望大家明白:
ConfigColorWithFont.swift :
let kMainColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 0.2)
func kRGBColor(_ red:CGFloat, _ green:CGFloat, _ blue:CGFloat) -> UIColor {
UIColor(red: red, green: green, blue: blue, alpha: 1)
}
func kRGBAColor(_ red:CGFloat, _ green:CGFloat, _ blue:CGFloat, _ alpha:CGFloat) -> UIColor {
UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
func kRandomColor() -> UIColor {
UIColor(red: CGFloat(Double(arc4random_uniform(256)) / 255.0), green: CGFloat(Double(arc4random_uniform(256)) / 255.0), blue: CGFloat(Double(arc4random_uniform(256)) / 255.0), alpha: 1.0)
}
func colorWithHexString(_ color: String) -> UIColor {
ConfigSupportTools.color(withHexString: color)//调用OC桥接方法
}
//字体
//闭包表达式定义函数
let kSystemFont = { (num: CGFloat) -> UIFont in
UIFont.systemFont(ofSize: num)
}
let kBoldFont = { num in
UIFont.systemFont(ofSize: num)
}
ConfigFrame.swift :
let kScreenW = UIScreen.main.bounds.size.width
let kScreenH = UIScreen.main.bounds.size.height
func fit_iPhone6_Width(_ x:CGFloat) -> CGFloat{
UIScreen.main.bounds.size.width * ((x)/375.0)
}
func fit_iPhone6_Height(_ x:CGFloat) -> CGFloat{
UIScreen.main.bounds.size.height * ((x)/667.0)
}
//TODO:未完成
#warning("TODO:未完待续")
ConfigDevice.swift :
//判断系统版本 应用版本
let kSystemVersion = Double(UIDevice.current.systemVersion)!
let kAppVersionCode = Bundle.main.infoDictionary!["CFBundleShortVersionString"]!
func isiOS9Later() -> Bool{
kSystemVersion >= 9
}
func isiOS10Later() -> Bool{
kSystemVersion >= 10
}
func isiOS11Later() -> Bool{
kSystemVersion >= 11
}
func isiOS12Later() -> Bool{
kSystemVersion >= 12
}
func isiOS13Later() -> Bool{
kSystemVersion >= 13
}
// 判断是否是 ipad
let kIsPad = {
UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
}()
// 判断 iPhone6系列 kIsiPhone_6_6s_7_8
let kIsiPhone_6 = {
UIScreen.main.currentMode?.size .equalTo(CGSize(width: 750, height: 1334))
}()!
// 判断iphone6P系列 kiIsPhone_6p_6sp_7p_8p
let kIsiPhone_6P = {
UIScreen.main.currentMode?.size .equalTo(CGSize(width: 1242, height: 2208))
}()!
// 判断iPhoneX XS
let kIsiPhone_X = {
UIScreen.main.currentMode?.size .equalTo(CGSize(width: 1125, height: 2436))
}()!
// 判断iPhoneXr
let kIsiPhone_Xr = {
UIScreen.main.currentMode?.size .equalTo(CGSize(width: 750, height: 1624))
}()!
// 判断iPhone XSMax
let kIsiPhone_XSMax = {
UIScreen.main.currentMode?.size .equalTo(CGSize(width: 1242, height: 2688))
}()!
// 是否是iPhonex系列手机
let kIsiPhoneXSeries = ConfigSupportTools.isNotchScreen()
ConfigCodeSimplify.swift :
import UIKit
//MARK: - 重要缩写
let kApplication = UIApplication.shared
let kAppDelegate = UIApplication.shared.delegate!
let kUserDefaults = UserDefaults.standard
let kNotificationCenter = NotificationCenter.default
//MARK: - 获取图片
let kImageName = { imageString in
UIImage(named: imageString)
}
//MARK: - 任何类型字符串转换
///Optional(String)
let kStringFormat = { (id:Any?) -> String? in
guard let value = id else {
return nil
}
return "\(value)"
}
//MARK: - 字符串初始化URL
func kUrlWithString(_ urlString:String) -> URL? {
URL(string: ConfigSupportTools.isContainChinese(urlString) ? (urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed))! : urlString)
}
//MARK: - debug模式输出函数
func kLog<T>(_ msg: T...,
file: NSString = #file,
line: Int = #line,
fn: String = #function) {
#if DEBUG
let prefix = "C:\(file.lastPathComponent) L:\(line) F:\(fn):"
print(prefix, msg)
#else
#endif
}