Swift标准库的高级扩展EZSwiftExtensions
Posted swift语言
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift标准库的高级扩展EZSwiftExtensions相关的知识,希望对你有一定的参考价值。
对Swift标准库、 Foundation、UIKit 提供了很多高级扩展函数,包括以下内容:
NSObject
Bool
Int
Double
String
NSAttritedString
Array
Dictioanry
NSDate
NSTimer
CGRect
UIViewController
UIView
UITextView
UILabel
UIImageView
UIImage
Block Objects
UIDevice
NSUserDefauts
NSURL EZ function and variables
示例代码:
通过索引获取对象
1 |
var myArray = [ "charmander" , "bulbasaur" , "squirtle" ] |
2 |
print(myArray. get ( 1 )) // "bulbasaur" |
随机获取对象
1 |
var myArray = [ "charmander" , "bulbasaur" , "squirtle" ] |
2 |
print(myArray.random()) // bulbasaur or something else |
通过对象获取索引
1 |
var myArray = [ "charmander" , "bulbasaur" , "squirtle" , "charmander" ] |
2 |
print(myArray.indexesOf( "charmander" )) // [0,3] |
检查数组中是否包含另一个数组
1 |
var myArray = [ "charmander" , "bulbasaur" , "squirtle" ] |
2 |
print(myArray.containsArray([ "charmander" , "bulbasaur" ])) // true |
3 |
print(myArray.containsArray([ "string" ])) // false |
切换布尔值
1 |
var myBool: Bool = true |
2 |
print(myBool.toggle()) // false |
初始化颜色
1 |
let myColor = UIColor(r: 100 , g: 100 , b: 100 ) // Default alpha is 1 |
延迟执行代码
1 |
Timer.runThisAfterDelay(seconds: 2 ) { () -> () in |
2 |
print( "Prints this 2 seconds later in main queue" ) |
3 |
} |
每秒执行代码
1 |
var count = 0 |
2 |
Timer.runThisEvery(seconds: 1 ) { (timer) -> Void in |
3 |
print( "Will print every second" ) |
4 |
if count == 3 { |
5 |
timer?.invalidate() |
6 |
} |
7 |
count += 1 |
8 |
} |
获取项目版本信息
1 |
print(ez.appVersion) // 0.3 |
2 |
print(ez.appBuild) // 7 |
3 |
print(ez.appVersionAndBuild) // v0.3(7) |
检测屏幕截图
1 |
ez.detectScreenShot { () -> () in |
2 |
print( "User took a screen shot" ) |
3 |
} |
更多请参见开源代码主页。
开源代码主页 ❤
文章来自:51swift
以上是关于Swift标准库的高级扩展EZSwiftExtensions的主要内容,如果未能解决你的问题,请参考以下文章