Swift-Swift中的全局變量和函數的創建


 解決辦法: 

 寫OC的時候常常會用到各種宏定義,但是Swift中貌似沒有宏的這種定義,更多的是通過全局常量或者全局函數來實現這一效果.我們只需要建立一個文件(假設為Macro.swift),把想用的定義在里面,無須導入頭文件什么的,就可以在全局用啦.

 

import UIKit
import Foundation

/**
*   替代oc中的#define,列舉一些常用宏
*/

// 屏幕的物理寬度
let kScreenWidth = UIScreen.mainScreen().bounds.size.width
// 屏幕的物理高度
let kScreenHeight = UIScreen.mainScreen().bounds.size.height

/**
*   除了一些簡單的屬性直接用常量表達,更推薦用全局函數來定義替代宏
*/
// 判斷系統版本
func kIS_IOS7() ->Bool { return (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 7.0 }
func kIS_IOS8() -> Bool { return (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 8.0 }

// 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
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM