对Swift标准库、 Foundation、UIKit 提供了很多高级扩展函数,包括以下内容:
示例代码:
通过索引获取对象
var myArray = ["charmander","bulbasaur","squirtle"] print(myArray.get(1)) // "bulbasaur"随机获取对象
var myArray = ["charmander","bulbasaur","squirtle"] print(myArray.random()) // bulbasaur or something else通过对象获取索引
var myArray = ["charmander","bulbasaur","squirtle","charmander"] print(myArray.indexesOf("charmander")) // [0,3]检查数组中是否包含另一个数组
var myArray = ["charmander","bulbasaur","squirtle"] print(myArray.containsArray(["charmander","bulbasaur"])) // true print(myArray.containsArray(["string"])) // false切换布尔值
var myBool: Bool = true print(myBool.toggle()) // false初始化颜色
let myColor = UIColor(r: 100, g: 100, b: 100) // Default alpha is 1延迟执行代码
Timer.runThisAfterDelay(seconds: 2) { () -> () in print("Prints this 2 seconds later in main queue") }每秒执行代码
var count = 0 Timer.runThisEvery(seconds: 1) { (timer) -> Void in print("Will print every second") if count == 3 { timer?.invalidate() } count += 1 }获取项目版本信息
print(ez.appVersion) // 0.3 print(ez.appBuild) // 7 print(ez.appVersionAndBuild) // v0.3(7)检测屏幕截图
ez.detectScreenShot { () -> () in print("User took a screen shot") }
更多请参见开源代码主页。