swift语言点评十六-Initialization && Deinitialization
initial value:必须初始化、不影响观察者
Classes and structuresmustset all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.
You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition. These actions are described in the following sections.
When you assign a default value to a stored property, or set its initial value within an initializer, the value of that property is set directly, without calling any property observers.
As with function and method parameters, initialization parameters can have both a parameter name for use within the initializer’s body and an argument label for use when calling the initializer.
Properties of optional type are automatically initialized with a value ofnil
,
常量可以在初始化时赋值;
条件:1、所有参数都有缺省参量;2、没有初始化函数;
结构体可以有以属性为参量的缺省构造器,类没有;
区分于引用类型:值类型、引用类型。
?没有明白?
self.init只能在初始器内部使用。
For value types, you useself.init
to refer to other initializers from the same value type when writing your own custom initializers. You can callself.init
only from within an initializer.