Revert "更新chapter4代码高亮"

This reverts commit 0068fdf92e.
This commit is contained in:
Junjie Tao
2014-08-21 15:35:31 +08:00
parent 0068fdf92e
commit ddf93756d8
5 changed files with 187 additions and 205 deletions

View File

@ -27,7 +27,7 @@
* [扩展](chapter2/20_Extensions.md)
* [协议](chapter2/21_Protocols.md)
* [泛型](chapter2/22_Generics.md)
* [权限控制](chapter2/23_Access_Control.md)
* [权限控制](chapter2/23_Access Control.md)
* [高级操作符](chapter2/24_Advanced_Operators.md)
* [语言参考](chapter3/chapter3.md)
* [关于语言参考](chapter3/01_About_the_Language_Reference.md)
@ -43,6 +43,3 @@
* [苹果官方Blog官方翻译](chapter4/chapter4.md)
* [Access Control 权限控制的黑与白](chapter4/01_Access_Control.md)
* [造个类型不是梦-白话Swift类型创建](chapter4/02_Type_Custom.md)
* [WWDC里面的那个“大炮打气球”](chapter4/03_Ballons.md)
* [Interacting with C Pointers 与C语言指针的交互](chapter4/04_Interacting_with_C_Pointers.md)
* [Swift里的值类型与引用类型](chapter4/05_Value and Reference Types.md)

View File

@ -40,7 +40,7 @@ Private私有级别的权限最严格它可以用来隐藏某些功能
除了可以给整个声明设权限Swift还允许大家在需要的时候把某个属性property的取值权限比赋值权限设得更加开放。
#####举个例子:
```swift
public class ListItem {
// ListItem这个类有两个公开的属性
@ -68,7 +68,7 @@ Private私有级别的权限最严格它可以用来隐藏某些功能
return false
}
}
```
当我们使用Objective-C和Swift混合开发时需要注意

View File

@ -21,12 +21,10 @@
来我们先看一下OCBool的定义。
#####代码示例如下:
```swift
enum OCBool{
case ocTrue
case ocFalse
}
```
1. enum OCBool{
2. case ocTrue
3. case ocFalse
4. }
#####注意:
@ -36,13 +34,12 @@ case ocFalse
<a name="imp-default"></a>
####实现默认值
我们给了一个漂亮的定义不过按照传统语言的经验Bool值默认情况下是假 所以我们的OCBool也应该如此我们使用类型扩展技术增加这个默认特性
```swift
extension OCBool{
init(){
self =.ocFalse
}
}
```
1. extension OCBool{
2. init(){
3. self =.ocFalse
4. }
5. }
#####注意:
- 代码中第1行extension关键字非常强大小伙伴们可以通过此创造出许多好玩的东西建议各位去Github上看一个名为“Swiftz”的项目它将扩展用到了极致。
@ -50,55 +47,50 @@ extension OCBool{
现在我们可以使用如下方法使用这个Bool类型。
#####代码示例如下:
```swift
var result:OCBool = OCBool()
var result1:OCBool = .ocTrue
```
1. var result:OCBool = OCBool()
2. var result1:OCBool = .ocTrue
<a name="init-by-bool"></a>
####支持基本布尔型初始化
正如上述代码所述我们只能通过类型或者枚举项目赋值这是组合类型的用法但是编码的日子里我们总是希望和truefalse直接打交道也就是说我们希望这么做
代码示例如下:
```swift
var isSuccess:OCBool = true
```
1. var isSuccess:OCBool = true
如果小伙伴们直接这么用,则会出现如下错误:
```
/Users/tyrion-OldCoder/Documents/Learning/BoolType/BoolType/main.swift:30:24: Type 'OCBool' does not conform to protocol 'BooleanLiteralConvertible'
```
1. /Users/tyrion-OldCoder/Documents/Learning/BoolType/BoolType/main.swift:30:24: Type 'OCBool' does not conform to protocol 'BooleanLiteralConvertible'
编译器咆哮的原因是,我们的类型没有遵从“布尔字面量转换协议”,接下来修正这个问题,
#####代码示例如下:
```swift
import Foundation
println("Hello, World!")
enum OCBool{
case ocTrue
case ocFalse
}
extension OCBool: BooleanLiteralConvertible{
static func convertFromBooleanLiteral( value: Bool) ->OCBool{
return value ? ocTrue : ocFalse
}
}
var isSuccess:OCBool = true
```
1. import Foundation
2.
3. println("Hello, World!")
4.
5. enum OCBool{
6. case ocTrue
7. case ocFalse
8. }
9.
10.
11. extension OCBool: BooleanLiteralConvertible{
12. static func convertFromBooleanLiteral( value: Bool) ->OCBool{
13. return value ? ocTrue : ocFalse
14. }
15. }
16.
17. var isSuccess:OCBool = true
#####注意:
- 代码中的第11行是重点我的类型OCBool支持了BooleanLiteralConvertible协议这个协到底是干什么的呢小伙伴们在Xcode代码编辑器按住Command键然后点击第11行中的BooleanLiteralConvertible协议名则会进入它的定义
#####其定义如下:
```swift
protocol BooleanLiteralConvertible {
typealias BooleanLiteralType
class func convertFromBooleanLiteral(value: BooleanLiteralType) -> Self
}
```
1. protocol BooleanLiteralConvertible {
2. typealias BooleanLiteralType
3. class func convertFromBooleanLiteral(value: BooleanLiteralType) -> Self
4. }
- 这个定义中有个类方法convertFromBooleanLiteral它的参数为BooleanLiteralType类型也就是我传入的Bool类型 且返回值为实现这个协议的类型本身在我们的OCBool类型中其返回值就是OCBool本身。经过这个定义我们可以直接对OCBool类型直接进行布尔字面量初始化了。
@ -106,66 +98,64 @@ protocol BooleanLiteralConvertible {
####支持Bool类型判断
小伙伴们不安分, 肯定想着我怎么用它实现逻辑判断,所以如果你这么写,
#####代码示例如下:
```swift
var isSuccess:OCBool = true
if isSuccess {
println( "老码请你吃火锅!")
}
```
1. var isSuccess:OCBool = true
2.
3. if isSuccess {
4. println( "老码请你吃火锅!")
5. }
你永远吃不到老码的火锅,因为这里编译器会咆哮:
```
/Users/tyrion-OldCoder/Documents/Learning/BoolType/BoolType/main.swift:27:4: Type 'OCBool' does not conform to protocol 'LogicValue'
```
1. /Users/tyrion-OldCoder/Documents/Learning/BoolType/BoolType/main.swift:27:4: Type 'OCBool' does not conform to protocol 'LogicValue'
OCBool现在只能用bool类型初始化而不能直接返回bool型小火把们还记得在《老码说编程之白话Swift江湖》中老码多次提到妈妈再也不担心我们 if a = 1{}的写法了, 因为等号不支持值返回了, 所以在if判断是后面的条件必须有返回值OCBool没有所以编译器哭了。我们解决这个问题。
#####代码示例如下:
```swift
import Foundation
println("Hello, World!")
enum OCBool{
case ocTrue
case ocFalse
}
extension OCBool: BooleanLiteralConvertible{
static func convertFromBooleanLiteral( value: Bool) ->OCBool{
return value ? ocTrue : ocFalse
}
}
extension OCBool: LogicValue{
func getLogicValue() ->Bool {
var boolValue: Bool{
switch self{
case .ocTrue:
return true
case .ocFalse:
return false
}
}
return boolValue
}
}
var isSuccess:OCBool = true
if isSuccess {
println( "老码请你吃火锅!")
}
```
1. import Foundation
2.
3. println("Hello, World!")
4.
5. enum OCBool{
6. case ocTrue
7. case ocFalse
8. }
9.
10.
11. extension OCBool: BooleanLiteralConvertible{
12. static func convertFromBooleanLiteral( value: Bool) ->OCBool{
13. return value ? ocTrue : ocFalse
14. }
15. }
16.
17. extension OCBool: LogicValue{
18. func getLogicValue() ->Bool {
19. var boolValue: Bool{
20. switch self{
21. case .ocTrue:
22. return true
23. case .ocFalse:
24. return false
25. }
26. }
27. return boolValue
28. }
29. }
30.
31.
32. var isSuccess:OCBool = true
33.
34. if isSuccess {
35. println( "老码请你吃火锅!")
36. }
####运行结果如下:
```
Hello, World!
老码请你吃火锅!
Program ended with exit code: 0
```
1. Hello, World!
2. 老码请你吃火锅!
3. Program ended with exit code: 0
#####注意:
- 如果小伙伴们现在用的是Beta版的Xcode注意苹果官方Blog中在代码第17行如果在Xcode Beta4下是错误的这里的协议是LogicValue而不是BooleanVue所以记得看错误提示才是好习惯。
- 注意代码第34行完美支持if判断且输出结果为“老码请你吃火锅”老码也是说说而已请不要当真。
@ -175,35 +165,34 @@ Program ended with exit code: 0
小伙伴们江湖风险门派众多老码有自己的OCBool类型可能嵩山少林有自己的SSBool类型甚至连郭美美都可能有自己的MMBool类型所以OCBool必须能够识别这些类型这些各门各派的类型只要支持LogicValue协议就应该可以被识别看老码怎么做
#####代码示例如下:
```swift
extension OCBool{
init( _ v: LogicValue )
{
if v.getLogicValue(){
self = .ocTrue
}
else{
self = .ocFalse
}
}
}
var mmResult: Bool = true
var ocResult:OCBool = OCBool(mmResult)
if ocResult {
println( "老码没钱,郭美美请你吃火锅!")
}
```
1. extension OCBool{
2. init( _ v: LogicValue )
3. {
4. if v.getLogicValue(){
5. self = .ocTrue
6. }
7. else{
8. self = .ocFalse
9. }
10. }
11.
12. }
13.
14. var mmResult: Bool = true
15. var ocResult:OCBool = OCBool(mmResult)
16.
17.
18. if ocResult {
19. println( "老码没钱,郭美美请你吃火锅!")
20. }
#####代码运行结果如下:
```
Hello, World!
老码没钱,郭美美请你吃火锅!
Program ended with exit code: 0
```
1. Hello, World!
2. 老码没钱,郭美美请你吃火锅!
3. Program ended with exit code: 0
漂亮我们的OCBool类型现在支持了所有的逻辑变量初始化。
#####注意:
@ -213,71 +202,69 @@ Program ended with exit code: 0
####完善OCBool的布尔基因体系
小伙伴们bool类型的价值就是在于各种判断诸如==!=, &|,^,!以及各种组合逻辑运算我们OCBool也要具备这些功能否则就会基因缺陷且看老码如何实现
```swift
extension OCBool: Equatable{
}
//支持等值判断运算符
func ==( left: OCBool, right: OCBool )->Bool{
switch (left, right){
case (.ocTrue, .ocTrue):
return true
default:
return false
}
}
//支持位与运算符
func &( left:OCBool, right: OCBool)->OCBool{
if left{
return right
}
else{
return false
}
}
//支持位或运算符
func |( left:OCBool, right: OCBool)->OCBool{
if left{
return true
}
else{
return right
}
}
//支持位异或运算符
func ^( left:OCBool, right: OCBool)->OCBool{
return OCBool( left != right )
}
//支持求反运算符
@prefix func !( a:OCBool )-> OCBool{
return a ^ true
}
//支持组合求与运算符
func &= (inout left:OCBool, right:OCBool ){
left = left & right
}
var isHasMoney:OCBool = true
var isHasWife:OCBool = true
var isHasHealty:OCBool = true
var isHasLover:OCBool = true
isHasMoney != isHasHealty
isHasHealty == isHasMoney
isHasWife ^ isHasLover
isHasWife = !isHasLover
if (isHasMoney | isHasHealty) & isHasHealty{
println( "人生赢家,就像老码一样!")
}else
{
println("人生最苦的事事,人死了钱没花了,人生最苦的事是,人活着,钱没了!")
}
```
1. extension OCBool: Equatable{
2. }
3.
4. //支持等值判断运算符
5. func ==( left: OCBool, right: OCBool )->Bool{
6. switch (left, right){
7. case (.ocTrue, .ocTrue):
8. return true
9. default:
10. return false
11. }
12. }
13. //支持位与运算符
14. func &( left:OCBool, right: OCBool)->OCBool{
15.
16. if left{
17. return right
18. }
19. else{
20. return false
21. }
22. }
23. //支持位或运算符
24. func |( left:OCBool, right: OCBool)->OCBool{
25.
26. if left{
27. return true
28. }
29. else{
30. return right
31. }
32. }
33.
34. //支持位异或运算符
35. func ^( left:OCBool, right: OCBool)->OCBool{
36. return OCBool( left != right )
37. }
38. //支持求反运算符
39. @prefix func !( a:OCBool )-> OCBool{
40. return a ^ true
41. }
42. //支持组合求与运算符
43. func &= (inout left:OCBool, right:OCBool ){
44. left = left & right
45. }
46.
47.
48. var isHasMoney:OCBool = true
49. var isHasWife:OCBool = true
50. var isHasHealty:OCBool = true
51. var isHasLover:OCBool = true
52.
53. isHasMoney != isHasHealty
54. isHasHealty == isHasMoney
55. isHasWife ^ isHasLover
56. isHasWife = !isHasLover
57.
58. if (isHasMoney | isHasHealty) & isHasHealty{
59. println( "人生赢家,就像老码一样!")
60. }else
61. {
62. println("人生最苦的事事,人死了钱没花了,人生最苦的事是,人活着,钱没了!")
63. }
好了到这里就到这里了窗外的雷声叫醒了老码现在应该去吃饭了以上老码给大家展示了如果制造一个自己的类型记得老码的示例是在Xcode6 Beta4下测试的至于Beta5的改变还没有涉及小伙伴们要好生练习以后各种自定类型都是基于这个思想。还有这个章节不是老码的原创老码认真的阅读了苹果的官方博客且自己的练习总结如果小伙伴们费了吃奶的劲还是看不懂请找度娘谷歌还是看不懂请到老码官方微博http://weibo.com/u/5241713117咆哮。

View File

@ -15,27 +15,25 @@ Swift里面的类型分为两种
值类型和引用类型最基本的分别在复制之后的结果。当一个值类型被复制的时候,相当于创造了一个完全独立的实例,这个实例保有属于自己的独有数据,数据不会受到其他实例的数据变化影响:
```swift
// 下面是一个值类型的例子
struct S { var data: Int = -1 }
var a = S()
var b = a // b是a的拷贝
a.data = 42 // 更改a的数据b的不受影响
println("\(a.data), \(b.data)") // 输出结果 "42, -1"
```
值类型就好像身份证复印件一样,复印出来之后,修改原件上面的内容,复印件上的内容不会变。
另一方面,复制一个引用类型的时候,实际上是默默地创造了一个共享的实例分身,两者是共用一套数据。因此修改其中任何一个实例的数据,也会影响到另外那个。
```swift
// 下面是一个引用类型的例子
class C { var data: Int = -1 }
var x = C()
var y = x // y是x的拷贝
x.data = 42 // 更改x的数据等于同时修改了y
println("\(x.data), \(y.data)") // 输出结果 "42, 42"
```
### Mutation变异在安全中扮演的角色