Currently I am studying the 3rd book "Swift Programming", I think it is a quite useful book.
I can send electronic copies of the book to you if you want (send me an e-mail).
dkychang@qq.com
fileprivate class IntArrayBuffer { var storage: [Int] init() { storage = [] } init(buffer: IntArrayBuffer) { storage = buffer.storage } } struct IntArray { private var buffer: IntArrayBuffer init() { buffer = IntArrayBuffer() } func describe() { print(buffer.storage) } }
//兩個(gè)init是獨(dú)立的,只是多了一個(gè)方式來創(chuàng)建IntArrayBuffer fileprivate class IntArrayBuffer { var storage: [Int] //這個(gè)沒有傳遞參數(shù),把storage初始化為空數(shù)組。 init() { storage = [] } //這個(gè)有一個(gè)IntArrayBuffer類型的參數(shù),storage初始化為buffer.storage init(buffer: IntArrayBuffer) { storage = buffer.storage } } struct IntArray { private var buffer: IntArrayBuffer init() { buffer = IntArrayBuffer() } func describe() { print(buffer.storage) } }下面的代碼展示如何使用IntArrayBuffer
private var bufferA = IntArrayBuffer() bufferA.storage = [1,2,3] //把bufferA.storage復(fù)制到bufferB.storage private var bufferB = IntArrayBuffer(buffer: bufferA) print(bufferB.storage) //print [1,2,3]
enum ShapeDimensions { // point has no associated value - it is dimensionless case point // square's associated value is the length of one side case square(side: Double) // rectangle's associated value defines its width and height case rectangle(width: Double, height: Double) func area() -> Double { switch self { case ShapeDimensions.point: return 0 case let ShapeDimensions.square(side: side): return side * side case let ShapeDimensions.rectangle(width: w, height: h): return w * h } } } var squareShape = ShapeDimensions.square(side: 10.0) var rectShape = ShapeDimensions.rectangle(width: 5.0, height: 10.0) var pointShape = ShapeDimensions.point print("square's area = \(squareShape.area())") print("rectangle's area = \(rectShape.area())") print("point's area = \(pointShape.area())")
struct Town { } class Monster { } class Zombie: Monster { var walksWithLimp: Bool init(limp: Bool, town: Town?){ walksWithLimp = limp } convenience init(limp: Bool) { self.init(limp: limp, town: nil) } deinit { print("ZZZZombie is no longer with us.") } } var convenientZombie = Zombie(limp: true) convenientZombie = Zombie(limp: true)
class Zombie { var walksWithLimp: Bool init(limp: Bool){ walksWithLimp = limp } deinit { print("ZZZZombie is no longer with us.") } } var convenientZombie = Zombie(limp: true) convenientZombie = Zombie(limp: true)
enum ShapeDimensions { case point //原文:case square(side: Double) //去掉了side case square(Double) //原文:case rectangle(width: Double, height: Double) //去掉了width,height case rectangle(Double, Double) func area() -> Double { switch self { case ShapeDimensions.point: return 0 //原文:case let ShapeDimensions.square(side: side): //原文中的side: side,2個(gè)side感覺有兩個(gè)變量名似的,看起來不好理解。 //因?yàn)樽钌厦媸沁@樣寫的:case square(side: Double) //所以這里我覺得應(yīng)該這樣寫:case let ShapeDimensions.square(side: Double): //但是這樣寫編譯不通過 //通過下面這樣的寫法就好理解為什么加let了,因?yàn)閟ide是個(gè)常量名,不能無中生有,必須用let定義出來。當(dāng)然let也可以改成var case let ShapeDimensions.square(side): return side * side //原文:case let ShapeDimensions.rectangle(width: w, height: h): //let加在case后面是這句的簡寫:case ShapeDimensions.rectangle(let w, let h): case let ShapeDimensions.rectangle(w, h): return w * h } } } var squareShape = ShapeDimensions.square(10.0) //原文:square(side: 10.0) var rectShape = ShapeDimensions.rectangle(5.0, 10.0) //原文:rectangle(width: 5.0, height: 10.0) var pointShape = ShapeDimensions.point print("square's area = \(squareShape.area())") print("rectangle's area = \(rectShape.area())") print("point's area = \(pointShape.area())")