Initializer
required
Use required to modify the specified initializer, indicating that all its subclasses must implement the initializer (implemented through inheritance or rewrite)
If the subclass overrides the required initializer, it must also be added with required without override
class Person { required init() {} init(age: Int) {} } class Student: Person { init(no: Int) { (age: 0) } required init() { () } }
Attribute Observer
Assigning values to the parent class in its own initializer will not trigger the property observer, but assigning values to the child class in its initializer will trigger the property observer.
class Person { var age: Int { willSet { print("willSet", newValue) } didSet { print("didSet", oldValue, age) } } init() { = 0 } } class Student: Person { override init() { () = 1 } }
Failed initializer
Can classes, structures, and enumerations be used? Definition of failed initializers
class Person { var name: String init?(name: String) { if { return nil } = name } }
Failable initializers and non-failable initializers with the same parameter label, number of parameters, and parameter types are not allowed to be defined at the same time.
Init! can define an implicit unpacked failed initializer
The failable initializer can call the non-failable initializer, and the non-failable initializer calls the failable initializer and need to be unpacked.
class Person { var name: String init?(name: String) { if { return nil } = name } convenience init() { (name: "")! } }
If the initializer calls a failed initializer, resulting in the initialization failure, the entire initialization process fails and the subsequent code stops executing
A failable initializer can be rewrited with a non-failable initializer, but the other way around is not.
Deinit
deinit is called a deinitizer, similar to the destructor of C++ and the dealloc method in OC
When the instance object of the class is released, the deinit method of the instance object is called
class Person { deinit { print("Person object destroyed") } }
deinit does not accept any parameters, cannot write brackets, and cannot call it by itself
The deinit of the parent class can be inherited by the subclass
After the implementation of the deinit of the subclass is completed, the deinit of the parent class will be called.
Optional Chaining
class Car { var price = 0 } class Dog { var weight = 0 } class Person { var name: String = "" var dog: Dog = Dog() var car: Car? = Car() func age() -> Int { 18 } func eat() { print("Person eat") } subscript(index: Int) -> Int { return index } } var person: Person? = Person() var age = person?.age()//Int? Optional(18) var age1 = person!.age() // Int var name = person?.name //String? var index = person?[6] // Int?
If the optional option is nil, the call to the method, subscript, and attribute fails, and the result is nil
If the optional option is not nil, the method, subscript, and attribute are called successfully, and the result will be wrapped as optional
If the result is an option, it will not be repackaged
Determine whether the method has been called successfully:
if let age = person?.age() { // ()? print("Call age was successful", age) } else { print("Call age failed") }
Form an optional chain:
Multiple? Can be linked together
If any node in the chain is nil, then the entire chain will fail to call, and there are still many places where optional chains are applied. In OC, we usually add a lot of judgments to avoid crashes. In Swift, because having optional chains will reduce our own judgments and improve security.
var dog = person?.dog // Dog? var weight = person?. // Int? var price = person?.car?.price // Int?
var scores = [ "Jack" : [86, 82, 84], "Rose" : [79, 94, 81] ] scores["Jack"]?[0] = 100 scores["Rose"]?[2] += 10 scores["Kate"]?[0] = 88
var num1: Int? = 5 num1? = 10 // Optional(10) var num2: Int? = nil num2? = 10 // nil
var dict: [String : (Int, Int) -> Int] = [ "sum" : (+), //Add two Int types to return an Int type "difference" : (-) ] var result = dict["sum"]?(10, 20) // Optional(30), Int?
This is the article about how to use Swift initializer and optional chains. For more related contents of Swift initializer and optional chains, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!