From 59d1e5bbc5c8f8a04e7782c84f8f72cd57cd65bd Mon Sep 17 00:00:00 2001 From: shanksyang Date: Sun, 1 Nov 2015 08:01:07 +0800 Subject: [PATCH] chapter2, 22, protocols, swift 2.1 --- source/chapter2/22_Protocols.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/source/chapter2/22_Protocols.md b/source/chapter2/22_Protocols.md index 4ae60072..6d4aa174 100644 --- a/source/chapter2/22_Protocols.md +++ b/source/chapter2/22_Protocols.md @@ -8,7 +8,9 @@ > 2.0 > 翻译:[futantan](https://github.com/futantan) > 校对:[小铁匠Linus](https://github.com/kevin833752) -> 定稿:[shanksyang](http://codebuild.me) + +> 2.1 +> 校对:[shanks](http://codebuild.me),2015-11-01 本页包含内容: @@ -175,7 +177,7 @@ print("And another one: \(generator.random())") ``` -## 对Mutating方法的规定 +## 对 Mutating 方法的规定 有时需要在方法中改变它的实例。例如,值类型(结构体,枚举)的实例方法中,将`mutating`关键字作为函数的前缀,写在`func`之前,表示可以在该方法中修改它所属的实例及其实例属性的值。这一过程在[在实例方法中修改值类型](./11_Methods.html#modifying_value_types_from_within_instance_methods)章节中有详细描述。 @@ -857,7 +859,7 @@ print("Here's a random number: \(generator.random())") print("And here's a random Boolean: \(generator.randomBool())") // 输出 "And here's a random Boolean: true" ``` - + ### 提供默认实现 可以通过协议扩展的方式来为协议规定的属性和方法提供默认的实现。如果协议的遵循者对规定的属性和方法提供了自己的实现,那么遵循者提供的实现将被使用。 @@ -875,6 +877,7 @@ extension PrettyTextRepresentable { } ``` + ### 为协议扩展添加限制条件 在扩展协议的时候,可以指定一些限制,只有满足这些限制的协议遵循者,才能获得协议扩展提供的属性和方法。这些限制写在协议名之后,使用`where`关键字来描述限制情况。([Where语句](./23_Generics.html#where_clauses))。: