加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 2.0语法更新(二)

发布时间:2020-12-14 02:03:22 所属栏目:百科 来源:网络整理
导读:2.Checking API Availability Swift有内置的API可用性检查,它保证了你不会误用在当前系统上不支持的API,如果你使用了则会在运行时报错,你可以使用#available 配合if 或guard 来实现。 if #available ( iOS 9 , OSX 10.10 ,*) { // Use iOS 9 APIs on iOS,
2.Checking API Availability

Swift有内置的API可用性检查,它保证了你不会误用在当前系统上不支持的API,如果你使用了则会在运行时报错,你可以使用#available 配合if 或guard 来实现。

if #available ( iOS 9 , OSX 10.10 ,*) {
// Use iOS 9 APIs on iOS,and use OS X v10.10 APIs on OS X
} else {
//Fall back to earlier iOS and OS X APIs
}
中间用逗号隔开,* 表示其它任何平台,写在括号内的表示可执行的系统的最低版本。你不能在可用性条件语句之间加上&& 或者|| 这样的运算符。

3.Early Exit

guard关键字和if 类似,语句的执行都是取决于布尔类型的真假,guard关键字必须要搭配else 一起使用。
如果guard 后的布尔类型为真,就会跳过else 继续执行,在guard 判断时所定义的变量在剩下的代码块中也是可用的;
如果判断为假,就会进入else 语句执行,这个分支必须起到终结代码块的作用,比如可以使用return ,break,throw 或者 continue,你也可以调用一个没返回值的函数,比如fatalError()。

4.Protocol Extension

协议可以被拓展来为那些声明满足协议的类型提供方法和变量。

extension RandomNumberGenerator {
func randomBool() -> Bool {
return random() > 0.5
}
}

当协议拓展时,所有满足该协议的类型都会自动获得添加的方法。

let generator = LinearCongruentialGenerator() //满足RandomNumberGenerator协议

print(
"Here's a random number ( generator .random() )" )
print("And here's a random Boolean: ( generator .randomBool)" )

你可以使用协议拓展来为所有满足协议的类型提供方法或变量的默认实现,如果某类型内部提供了它自己的实现方法,那么协议提供的默认方法就不会被调用,而是采用其内部定义的方法。(译者注:类似于类中子类对父类函数的重载)

你也可以使用where关键字来为协议的拓展增加限制。

CollectionType where Generator.Element : TextRepresentable {
func asList() -> String {
return ""
}
}

上述代码是为 元素满足TextRepresentable 协议的Generator 类型添加asLis() 方法,当然Generator 需要满足CollectionType 协议。

5.Where Clauses

protocol Container {
typealias ItemType
mutating func append (item: ItemType )
var count: Int { get }
subscript (i: Int ) -> ItemType { get }
}

func allItemMatch<C1: Container,C2: Container where C1.ItemType == C2.ItemType,C1.ItemType: Equatable> (someContainer: C1 ,162)">_ anotherContainer: C2 ) -> if someContainer. count != anotherContainer. count {
return false
}
for i in 0 ..<someContainer. if someContainer[i] != anotherContainer[i] {
false
}
}
true
}

如上述代码,C1与C2是泛型,C1C2的具体类型不必相同,但必须都满足Container协议,而且C1.ItemType需要与C2的ItemType相同,ItemType需要满足Equatable协议。

补充:

Array : Container { }

该协议可以直接使数组类型满足,通过查看定义可知数组是结构体类型,在数组类型的定义中存在append() 和 subscript满足协议要求,虽然数组的定义中没有ItemType类型,但Swift可通过协议定义配合数组本身的定义推测出ItemType的类型。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读