Object-oriented targets
- constructor function
- Basic concepts of constructors
- Execution order of constructors
KVC
The use and principle of the structure function
- Convenient constructor function
- Destructors
- Distinguishing
重载
and重写
- Lazy Loading
- Read-only properties (computed properties)
- Set model data (
didSet
)
Constructor Basics
构造函数
is a special function that is used primarily to initialize an object when it is created, to set an initial value for an object, the 成员变量
constructor in OC is Initwithxxx, and in Swift, because of the support for function overloading , all constructors areinit
function of the constructor
- Allocate space
alloc
- Set the initial value
init
Required Properties
class Person: NSObject { /// 姓名 var name: String /// 年龄 var age: Int}
Prompt Error- Class ‘Person‘ has no initializers
‘Person‘ 类没有实例化器s
Cause: If a required attribute is defined in a class, you must allocate space for these required properties through the constructor and set the initial value
重写
The constructor of the parent class
/// `重写`父类的构造函数override init() {}
Prompt Error- Property ‘self.name‘ not initialized at implicitly generated super.init call
属性 ‘self.name‘ 没有在隐式生成的 super.init 调用前被初始化
- Adding
super.init()
calls Manually
/// `重写`父类的构造函数override init() { super.init()}
Prompt Error- Property ‘self.name‘ not initialized at super.init call
属性 ‘self.name‘ 没有在 super.init 调用前被初始化
- Set the initial value for the required property
/// `重写`父类的构造函数override init() { name = "张三" age = 18 super.init()}
Summary
- Non-Optional property, you must set the initial value in the constructor to ensure that the property is correctly initialized when the object is instantiated.
- Before calling the parent class constructor, you must ensure that the properties of this class have been initialized
- The constructor in Swift does not have to be written
func
Constructors for subclasses
- When customizing a subclass, you need to set the initial values for the properties defined in this class first in the constructor
- Then call the parent class's constructor to initialize the properties defined in the parent class.
/// 学生类class Student: Person { /// 学号 var no: String override init() { no = "001" super.init() }}
Summary
- The constructors of this class are called first to initialize the properties of this class
- Then call the parent class's constructor to initialize the properties of the parent class
- After Xcode 7 Beta 5, the constructor of the parent class is automatically called, strongly recommended
super.init()
, and the readability of the code execution thread is maintained
super.init()
Must be placed after the initialization of this class of properties to ensure all initialization of this class of properties is complete
Optional
Property
- Set the object property type to
Optional
class Person: NSObject { /// 姓名 var name: String? /// 年龄 var age: Int?}
可选属性
Does not need to set the initial value, the default initial value is nil
可选属性
Allocates space when setting values, is deferred allocation space, more in line with the principle of delayed creation in mobile development
Overloaded constructors
- Support for function overloading in Swift, same function name, different parameter types
/// `重载`构造函数////// - parameter name: 姓名/// - parameter age: 年龄////// - returns: Person 对象init(name: String, age: Int) { self.name = name self.age = age super.init()}
Precautions
- If the constructor is overloaded, but the default constructor is not implemented
init()
, the system no longer provides a default constructor
- Cause, when instantiating an object, you must allocate space and set an initial value through the constructor for the object property, and for the class that has the required parameters, the default
init()
cannot complete the allocation space and set the initial value of the work
To adjust the constructor of a subclass
重写
The constructor of the parent class
/// `重写`父类构造函数////// - parameter name: 姓名/// - parameter age: 年龄////// - returns: Student 对象override init(name: String, age: Int) { no = "002" super.init(name: name, age: age)}
/// `重载`构造函数////// - parameter name: 姓名/// - parameter age: 年龄/// - parameter no: 学号////// - returns: Student 对象init(name: String, age: Int, no: String) { self.no = no super.init(name: name, age: age)}
Note: If you are overloading a constructor, you must super
work with the initialization of the parent class property to complete
重载
And
重写
重载
, function name is the same, parameter name/parameter type/parameter number is different
- Overloaded functions are not just limited to
构造函数
- Function overloading is an important symbol of face object programming language
- Function overloading simplifies programmer's memory
- OC does not support function overloading, which is an alternative to OC
withXXX...
重写
, subclasses need to be extended on the basis of the parent class's own methods, requiring the override
keyword
Simple syntax for Swift's entry (v)