当你为存储型属性设置默认值或者在构造器中为其赋值时,它们的值是被直接设置的,不会触发任何属性观测器(property observers)。
一、基本语法
class Human{ var name :String init(){ name = "human" } init(name:String){ self.name = name; } } var a = Human(name:"小笨狼")
class Human{ var name :String? } var a = Human() //这时候初始化调用的是默认构造器,编译器会自动生成一个构造器,将name=nil struct Student{ let name:String? let score = 0 } var b = Student(name:"小笨狼",score:10) //结构体默认构造器是逐一成员构造器
class Human{ var name :String? init(name:String){ //指定构造器 self.name = name } convenience init(){ //便利构造器 self.init(name:"小笨狼") } }
class Student:Human{ var score:Int init(name: String,score:Int){ self.score = score super.init(name:name) } convenience init() { self.init(name:"小笨狼",score:98) } }
class student{ var score:Int[] = { var scores:Int[] = Array() for m in 0...3{ scores.append(m) } return scores }() }
Swift学习笔记十四:构造(Initialization),布布扣,bubuko.com
Swift学习笔记十四:构造(Initialization)
原文:http://blog.csdn.net/xbenlang/article/details/32138861