校正了最近的提交内容
03_revision_history.md - 部分内容校正 05_Value_and_Reference_Types.md - 删除多余的超链接 06_access_control_&_protected.md - 校正内容并提交 SUMMARY.md - 增加06_access_control_&_protected.md
This commit is contained in:
@ -45,4 +45,5 @@
|
|||||||
* [造个类型不是梦-白话Swift类型创建](chapter4/02_Type_Custom.md)
|
* [造个类型不是梦-白话Swift类型创建](chapter4/02_Type_Custom.md)
|
||||||
* [WWDC里面的那个“大炮打气球”](chapter4/03_Ballons.md)
|
* [WWDC里面的那个“大炮打气球”](chapter4/03_Ballons.md)
|
||||||
* [Swift与C语言指针友好合作](chapter4/04_Interacting_with_C_Pointers.md)
|
* [Swift与C语言指针友好合作](chapter4/04_Interacting_with_C_Pointers.md)
|
||||||
* [引用类型和值类型的恩怨](chapter4/05_Value_and_Reference_Types.md)
|
* [引用类型和值类型的恩怨](chapter4/05_Value_and_Reference_Types.md)
|
||||||
|
* [访问控制和protected](chapter4/06_access_control_&_protected.md)
|
||||||
@ -32,19 +32,19 @@
|
|||||||
<td scope="row">2014-08-18</td>
|
<td scope="row">2014-08-18</td>
|
||||||
<td><ul class="list-bullet">
|
<td><ul class="list-bullet">
|
||||||
<li><p>
|
<li><p>
|
||||||
在协议中关于<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-XID_397">初始化需求</a>部分增加一个新的小节</p>
|
在章节协议中,增加新的小节:<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-XID_397">对构造器的规定(Initializer Requirements)</a></p>
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
为<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-XID_409">“类专有”协议集</a>增加了一个新的小节</p>
|
在章节协议中,增加新的小节:<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-XID_409">类专属协议(class-only protocols)</a></p>
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_494">断言</a>现在可以使用字符串内插语法,并删除了文档中有冲突的注释</p>
|
<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_494">断言(assertions)</a>现在可以使用字符串内插语法,并删除了文档中有冲突的注释</p>
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
更新了<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-XID_428">连接字符串和字符</a>小节来说明一个事实,那就是字符串和字符不能再用<code>+</code>号运算符或者复合加法运算符<code>+=</code>连接,这两种运算符现在只能用于字符串相连。请使用<code>String</code>类型的<code>append</code>方法在一个字符串的尾部增加单个字符</p>
|
更新了<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-XID_428">连接字符串和字符(Concatenating Strings and Characters)</a>小节来说明一个事实,那就是字符串和字符不能再用<code>+</code>号运算符或者复合加法运算符<code>+=</code>相互连接,这两种运算符现在只能用于字符串之间相连。请使用<code>String</code>类型的<code>append</code>方法在一个字符串的尾部增加单个字符</p>
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
在<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/doc/uid/TP40014097-CH35-XID_516">属性声明</a>章节增加了关于<code>availability</code>属性的一些信息</p>
|
在<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/doc/uid/TP40014097-CH35-XID_516">属性声明(Declaration Attributes)</a>章节增加了关于<code>availability</code>属性的一些信息</p>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</td>
|
</td>
|
||||||
@ -91,16 +91,16 @@
|
|||||||
重写了<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html#//apple_ref/doc/uid/TP40014097-CH17-XID_293">继承(Inheritance)</a> 这一章:删除了本章中关于构造器重写的介绍性报道;转而将更多的注意力放到新增的部分——子类的新功能,以及如何通过重写(overrides)修改已有的功能。另外,小节 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html#//apple_ref/doc/uid/TP40014097-CH17-XID_301">重写属性的Getters和Setters(Overriding Property Getters and Setters)</a> 中的例子已经被替换为展示如何重写一个 <code>description</code> 属性. (而关于如何在子类的构造器中修改继承属性的默认值的例子,已经被移到 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html#//apple_ref/doc/uid/TP40014097-CH17-XID_293">构造过程(Initialization)</a> 这一章.)
|
重写了<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html#//apple_ref/doc/uid/TP40014097-CH17-XID_293">继承(Inheritance)</a> 这一章:删除了本章中关于构造器重写的介绍性报道;转而将更多的注意力放到新增的部分——子类的新功能,以及如何通过重写(overrides)修改已有的功能。另外,小节 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html#//apple_ref/doc/uid/TP40014097-CH17-XID_301">重写属性的Getters和Setters(Overriding Property Getters and Setters)</a> 中的例子已经被替换为展示如何重写一个 <code>description</code> 属性. (而关于如何在子类的构造器中修改继承属性的默认值的例子,已经被移到 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html#//apple_ref/doc/uid/TP40014097-CH17-XID_293">构造过程(Initialization)</a> 这一章.)
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
更新了 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_331">构造器的继承与重写(Initializer Inheritance and Overriding)</a> 小节以标示: 重写一个特定的构造器必须使用 <code>override</code> 修改器.
|
更新了 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_331">构造器的继承与重写(Initializer Inheritance and Overriding)</a> 小节以标示: 重写一个特定的构造器必须使用 <code>override</code> 修饰符.
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
更新 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_339"> Required构造器(Required Initializers)</a> 小节以标示:<code>required</code> 修改器现在需要出现在所有子类的required构造器的声明中, 而required构造器的实现,现在可以仅从父类自动继承。
|
更新 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_339"> Required构造器(Required Initializers)</a> 小节以标示:<code>required</code> 修饰符现在需要出现在所有子类的required构造器的声明中, 而required构造器的实现,现在可以仅从父类自动继承。
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
中置(Infix)的 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_80">运算符函数(Operator Functions)</a> 不再需要<code>@infix</code> 属性.
|
中置(Infix)的 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_80">运算符函数(Operator Functions)</a> 不再需要<code>@infix</code> 属性.
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/RevisionHistory.html#//apple_ref/doc/uid/TP40014097-CH40-XID_1631">前置和后置运算符(Prefix and Postfix Operators)</a>的<code>@prefix</code> 和 <code>@postfix</code> 属性,已变更为 <code>prefix</code> 和 <code>postfix</code> 声明修改器(declaration modifiers).
|
<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/RevisionHistory.html#//apple_ref/doc/uid/TP40014097-CH40-XID_1631">前置和后置运算符(Prefix and Postfix Operators)</a>的<code>@prefix</code> 和 <code>@postfix</code> 属性,已变更为 <code>prefix</code> 和 <code>postfix</code> 声明修饰符(declaration modifiers).
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
增加一条注解:当Prefix和postfix运算符被作用于同一个操作数时,关于<a href="AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_81" data-id="//apple_ref/doc/uid/TP40014097-CH27-XID_81">前置和后置运算符(Prefix and Postfix Operators)</a>的顺序(postfix运算符会先被执行)
|
增加一条注解:当Prefix和postfix运算符被作用于同一个操作数时,关于<a href="AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_81" data-id="//apple_ref/doc/uid/TP40014097-CH27-XID_81">前置和后置运算符(Prefix and Postfix Operators)</a>的顺序(postfix运算符会先被执行)
|
||||||
@ -109,10 +109,10 @@
|
|||||||
在运算符函数(Operator functions)中, <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_82" data-id="//apple_ref/doc/uid/TP40014097-CH27-XID_82">组合赋值运算符(Compound Assignment Operators)</a> 不再使用 <code>@assignment</code> 属性来定义函数.
|
在运算符函数(Operator functions)中, <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_82" data-id="//apple_ref/doc/uid/TP40014097-CH27-XID_82">组合赋值运算符(Compound Assignment Operators)</a> 不再使用 <code>@assignment</code> 属性来定义函数.
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
在这个版本中,在定义<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_85">自定义操作符(Custom Operators)</a> 时,<b>修改器(Modifiers)的出现顺序发生变化</b>。比如, 现在,你该编写 <code>prefix operator</code>, 而不是 <code>operator prefix</code>.
|
在这个版本中,在定义<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_85">自定义操作符(Custom Operators)</a> 时,<b>修饰符(Modifiers)的出现顺序发生变化</b>。比如, 现在,你该编写 <code>prefix operator</code>, 而不是 <code>operator prefix</code>.
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
增加信息:关于<code>dynamic</code> 声明修改器(declaration modifier),于章节 <a href="Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-XID_705" data-id="//apple_ref/doc/uid/TP40014097-CH34-XID_705">声明修改器(Declaration Modifiers)</a>.
|
增加信息:关于<code>dynamic</code> 声明修饰符(declaration modifier),于章节 <a href="Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-XID_705" data-id="//apple_ref/doc/uid/TP40014097-CH34-XID_705">声明修饰符(Declaration Modifiers)</a>.
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
增加信息:<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-XID_886">字面量Literals</a> 的类型推导(type inference)
|
增加信息:<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-XID_886">字面量Literals</a> 的类型推导(type inference)
|
||||||
@ -168,7 +168,7 @@
|
|||||||
更新章节 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_453">类型标注(Type Annotations)</a> :多个相关变量可以用“类型标注”(type annotaion)在同一行中声明为同一类型。
|
更新章节 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_453">类型标注(Type Annotations)</a> :多个相关变量可以用“类型标注”(type annotaion)在同一行中声明为同一类型。
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
<code>@optional</code>, <code>@lazy</code>, <code>@final</code>, <code>@required</code> 等关键字被更新为 <code>optional</code>, <code>lazy</code>, <code>final</code>, <code>required</code> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-XID_705">参见声明修改器(Declaration Modifiers)</a>.
|
<code>@optional</code>, <code>@lazy</code>, <code>@final</code>, <code>@required</code> 等关键字被更新为 <code>optional</code>, <code>lazy</code>, <code>final</code>, <code>required</code> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-XID_705">参见声明修饰符(Declaration Modifiers)</a>.
|
||||||
</li>
|
</li>
|
||||||
<li><p>
|
<li><p>
|
||||||
更新整本书 —— 引用 <code>..<</code> 作为<a href="BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-XID_128" data-id="//apple_ref/doc/uid/TP40014097-CH6-XID_128">区间运算符(Half-Open Range Operator)</a> (取代原先的<code>..</code> ).
|
更新整本书 —— 引用 <code>..<</code> 作为<a href="BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-XID_128" data-id="//apple_ref/doc/uid/TP40014097-CH6-XID_128">区间运算符(Half-Open Range Operator)</a> (取代原先的<code>..</code> ).
|
||||||
|
|||||||
@ -9,9 +9,7 @@
|
|||||||
- [值类型与引用类型的区别](#difference-two)
|
- [值类型与引用类型的区别](#difference-two)
|
||||||
- [Mutation(修改)在安全中扮演的角色](#act-in=mutation)
|
- [Mutation(修改)在安全中扮演的角色](#act-in=mutation)
|
||||||
- [如何选择类型](#how-to-choose)
|
- [如何选择类型](#how-to-choose)
|
||||||
- [支持Bool类型判断](#condition-by-bool)
|
|
||||||
- [支持兼容各们各派的类型](#support-all-type)
|
|
||||||
- [完善OCBool的布尔基因体系](#make-up-type)
|
|
||||||
|
|
||||||
### Swift里面的类型分为两种:
|
### Swift里面的类型分为两种:
|
||||||
|
|
||||||
|
|||||||
41
source/chapter4/06_access_control_&_protected.md
Normal file
41
source/chapter4/06_access_control_&_protected.md
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
> 翻译:[老码团队翻译组-Arya](http://weibo.com/littlekok/)
|
||||||
|
> 校对:[老码团队翻译组-Jame](http://weibo.com/u/5241713117)
|
||||||
|
|
||||||
|
# 访问控制和protected
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
|
||||||
|
原文再续,书折第一回。
|
||||||
|
|
||||||
|
很多其他编程语言都有一种”protected“设定,可以限制某些类方法只能被它的子类所使用。
|
||||||
|
|
||||||
|
Swift支持了访问控制后,大家给我们的反馈都很不错。而有的开发者问我们:“为什么Swift没有类似protected的选项?”
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
**当我们在设计Swift访问控制的不同等级时,我们认为有两种主要场景:**
|
||||||
|
|
||||||
|
* 在一个APP里:隐藏某个类的私密细节。
|
||||||
|
* 在一个开源框架里:不让导入这个框架的APP,随便接触框架的内部实现细节。
|
||||||
|
|
||||||
|
上面的两种常见情况,对应着private和internal这两个等级。
|
||||||
|
|
||||||
|
而protected相当于把访问控制和继承特性混在一起,把访问控制的等级设定增加了一个维度,使之复杂化。即使设定了protected,子类还是可以通过新的公开方法、新的属性来接触到所谓“protected”了的API。另一方面,我们可以在各种地方重写一个方法,所谓的保护却没有提供优化机制。这种设定往往在做不必要的限制 一 protected允许了子类,但又禁止所有其他别的类(包括那些帮助子类实现某些功能的类)接触父类的成员。
|
||||||
|
|
||||||
|
有的开发者指出,apple的框架有时候也会把给子类用的API分隔出来。这时候protected不就有用了吗?我们研究后发现,这些方法一般属于下面两种情况:一是这些方法对子类以外的类没啥用,所以不需要严格保护(例如上面说的协助实现某些功能的类)。二是这些方法就是设计出来被重写,而不是直接用的。举个例子,`drawRect(_:) `就是在UIKit基础上使用的方法,但它不能在UIKit以外应用。
|
||||||
|
|
||||||
|
除此之外,如果有了protected,它要怎么样和extension相互作用呢?一个类的extension能接触它的protected成员吗?一个子类的extension可以接触父类的protected成员吗?extension声明的位置对访问控制等级有没有影响呢?(复杂到要哭了是不是?)
|
||||||
|
|
||||||
|
对访问控制的设计,也依循了Objective-C开发者(包括apple内外的)的常规做法。Objective-C方法和属性一般在.h头文件里声明,但也可以写在.m实现文件里。假如有一个公开的类,想把里面某些部分设为只有框架内可以获取时,开发者一般会创建另一个头文件给内部使用。以上三种访问级别,就对应了Swift里面的public,private和internal。
|
||||||
|
|
||||||
|
Swift的访问控制等级和继承无关,是单维度、非常清楚明了的。我们认为这样的模式更简洁,同时满足了最主要的需求:将一个类、或一个框架的实现细节隔离保护起来。这可能和你以前用过的不同,但我们鼓励你试试看。
|
||||||
|
|
||||||
|
-----------------
|
||||||
|
本章节不是老码的原创,老码认真的阅读了苹果的官方博客,且自己的练习总结,如果小伙伴们费了吃奶的劲还是看不懂,请找度娘谷歌,还是看不懂请到老码[官方微博](http://weibo.com/u/5241713117)咆哮。
|
||||||
|
|
||||||
|
##### 本文由翻译自Apple Swift Blog :[access control & protected](原文地址:https://developer.apple.com/swift/blog/)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user