OC中的宏定義
- C語言的宏定義, 并不是很嚴(yán)謹(jǐn), 任何代碼段都可以進(jìn)行宏定義, 甚至是無效的無序的亂碼也可以.
- 因為C語言的機(jī)制下, 是在編譯代碼時將宏直接替換成定義的代碼, 在實際使用中是存有安全隱患的
- 一般的宏定義函數(shù)或者是計算時, 要在外面加括號, 防止一些錯誤的出現(xiàn)
#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"宏定義"
- 設(shè)置全局常量, 簡單宏, 直接let 加常量名即可,
- 復(fù)雜的宏由于必須保證宏的代碼的語句的合法性其监,使用函數(shù)進(jìn)行實現(xiàn)
Swift的宏定義在本質(zhì)上并不應(yīng)該稱之為宏定義, 只是為了方便大家的理解, 實質(zhì)上是一些全局常量和函數(shù)
下面是我根據(jù)平常OC使用到的宏定義進(jìn)行一定的改寫,有些全局函數(shù)或常量并無必要摇肌,僅僅做學(xué)習(xí)用契沫,并非代表寫法更好翠勉,希望大家明白:
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)//調(diào)用OC橋接方法
}
//字體
//閉包表達(dá)式定義函數(shù)
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:未完待續(xù)")
ConfigDevice.swift :
//判斷系統(tǒng)版本 應(yīng)用版本
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系列手機(jī)
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: - 任何類型字符串轉(zhuǎn)換
///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模式輸出函數(shù)
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
}