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) } }
//两个init是独立的,只是多了一个方式来创建IntArrayBuffer fileprivate class IntArrayBuffer { var storage: [Int] //这个没有传递参数,把storage初始化为空数组。 init() { storage = [] } //这个有一个IntArrayBuffer类型的参数,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复制到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个side感觉有两个变量名似的,看起来不好理解。 //因为最上面是这样写的:case square(side: Double) //所以这里我觉得应该这样写:case let ShapeDimensions.square(side: Double): //但是这样写编译不通过 //通过下面这样的写法就好理解为什么加let了,因为side是个常量名,不能无中生有,必须用let定义出来。当然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())")