发现swift和java有一个完全不一样的地方@H_502_3@ @H_502_3@ 在swift中,子类必须先初始化子类的所有属性,然后才能调用父类的构造器. 而在java中.super调用必须出现在构造函数的第一行.@H_502_3@ @H_502_3@ java代码
public class Dog { String name; Dog(String name){ this.name = name; } } class NoisyDog extends Dog { int age; NoisyDog(String name) { // 交换以下两行的顺序会报错: Constructor call must be the first statement in a constructor super(name); this.age = 5; } }
对应的swift代码:
class Dog { var name: String; init(name: String){ self.name = name; } } class NoisyDog: Dog { var age: Int override init(name: String) { //交换以下两行的顺序会报错error: property 'self.age' not initialized at super.init call self.age = 5; super.init(name: name); } }
书中关于failable initializer描述有错误@H_502_3@ @H_502_3@ 以下代码在swift2.1及之前会编译错误,在swift2.2中修正了这个bug@H_502_3@ swift2.2: 子类failable designated 构造器在返回nil前不必初始化子类的属性也不必调用父类的designated initializer,也就是说,在子类的failable initilizer中允许提前返回nil
//: Playground - noun: a place where people can play import Foundation class Dog{ var name: String init(name: String){ self.name = name } } class NoisyDog : Dog { var age: Int override init(name: String){ self.age = 5 super.init(name: name) } init?(name: String,age: Int){ // as of swift2.2: 子类failable designated 构造器在返回nil前不必初始化子类的属性 // 也不必调用父类的designated initializer if age < 0 { return nil } self.age = age; super.init(name: name) } }
见: http://stackoverflow.com/questions/26495586/best-practice-to-implement-a-failable-initializer-in-swift/26497229#26497229