13. Constructors for classes
1. Introduction to Constructors
- The constructor is similar to the initialization method in OC: Init method
- When a class is created by default, a constructor is inevitably called
- Even if you do not write any constructors, the compiler provides a default constructor.
- If it is inherited from NSObject, you can override the constructor of the parent class
2. Basic use of constructors
2.1 Basic use of constructors
- The properties of the class must have a value
- If the value is not initialized at the time of definition, you can assign a value in the constructor
class person:nsobject var name:stringvar age:int // overridden the construction method of the NSObject (parent class) Override "0}}//Create a Person object = person ()
2.2 Assigning values to attributes when initializing
- Many times, when we create an object, we assign a value to the property
- Constructors can be customized
- Note: If you customize the constructor, the Init () method is overwritten. That is, there is no default constructor
class person:nsobject var name:stringvar age:int // Custom Constructors Override the Init () function == Age }}// Create a Person object "Ryan" )
2.3 Dictionary to model (incoming dictionary when initialized)
- When you actually create an object, it's more about turning the dictionary into a model
Attention:
- Go to the dictionary to take out the nsobject, any type.
- Can be converted to the desired type by as!, and then assigned (cannot be directly assigned)
classperson:nsobject var name:stringvar age:int//Custom Constructors Override the Init () functionInit (dict:String:NSObject) name= Dict"name" as!Stringage= Dict" Age" as!Int}}//Create a Person objectLet dict="name":"Ryan"," Age": -Let p= Person (dict:dict)
2.4 Dictionary to model (using KVC conversion)
- It is more convenient to use KVC dictionary to turn model
Attention:
classPerson:nsobject//the type of struct or class, which must be an optional type. Because there is no guarantee that the value will be assignedvar name:string?//The base data type cannot be an optional type, otherwise KVC cannot be convertedvar age:int=0//Custom Constructors Override the Init () functionInit (dict:String:NSObject)//object must be initialized firstSuper.init ()//calling the object's KVC method dictionary to modelsetvaluesforkeyswithdictionary (Dict)}}//Create a Person objectLet dict="name":"Ryan"," Age": -Let p= Person (dict:dict)
14. Closures
1. Introduction of closures
2. Use of closures
Review of usage of 2.1 block
- Classes that define network requests
@interfacehttptool:nsobject-(void) Loadrequest: (void(^) ()) Callbackblock; @end@implementationhttptool-(void) Loadrequest: (void(^) ()) Callbackblock {Dispatch_async (Dispatch_get_global_queue (0,0), ^{NSLog (@"Load Network data:%@", [Nsthread CurrentThread]);d Ispatch_async (Dispatch_get_main_queue (),^{callbackblock (); }); });}@end
- Make network requests, request to data and use block for callback
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *)event {[ Self.httptool loadrequest:^{NSLog (@ " main thread, callback data.%@", [nsthread CurrentThread]); }];}
block notation: type: return value (the name of the block) (parameter of block) value:^(parameter list) { // execute code };
2.2 Using closures instead of block
- Classes that define network requests
class Httptool:nsobject {func loadrequest (callBack: ()) {Dispatch_async (Dispatch_get_global_queue ( 00 in print (" load Data " in callBack ()}) }}}
Override event: Uievent? { // network request in print (" back to main thread ") , Nsthread.currentthread ());} ) }
2.3Closure of the summary of the wording:
2.3.1 Closure of the wording:
Type: (parameter list)--(return value) Tip: Beginner definition closure Type, direct write (), in// execute code }
shorthand for 2.3.2 closures
httptool.loadrequest ({ print (" back to main thread ", Nsthread.currentthread () ); })
httptool.loadrequest () { print (" back to main thread ", Nsthread.currentthread ()); } // This is the recommended formulation in development httptool.loadrequest { print (" back to main thread ", Nsthread.currentthread ()); }
2.3.3
circular references for closures
- If the closure is strongly referenced in the Httptool, a circular reference is formed
class httptool:nsobject {// define attribute to strongly reference the incoming closure var callBack: (())? func loadrequest (callBack: ()) {Dispatch_async (dispatch_get_global_queue (00 In print (" load Data "in = CallBack}}
// weak var weakself = self; // [Weak self] ()- in // [unowned Self] ()- in inch = Uicolor.redcolor () print ("Back to Main thread", Nsthread.currentthread ()); }
15 Lazy Loading
1. Introduction to Lazy Loading
2. Use of lazy loading
Lazy var variable: type = {Create variable code} ()
// The essence of lazy loading is that the closure is executed at first use, and the return value of the closure is assigned to the property // The function of lazy is to only assign a value once Lazy var array: [String] = { in return ["Ryan" "szy""Jack"] } ()
Swift Fundamentals 4