In programming, we often use
懒加载
, as the name implies, is used to open up space, such as the most commonly used controls in iOS development
UITableView
, we usually write this way when we implement the data source method objective-c
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{ return self.dataArray.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ // Xwshopcell *cell = [Xwshopcell cellwithtableview:tableview]; // cell.wine = Self.dataarray[indexpath.row]; // return cell;}
In the code above,
return Self.dataArray.count;
is actually using
@property (nonatomic, strong) Nsarray *dataarray;
@property
Attribute , a property is generated get
for the and the set
method, and here is the call to the get
method, but the return self.dataArray.count
code above is called
return _dataarray}
This call, if the member property DataArray Start No assignment, then in use, call get method, do not rewrite, will error, null pointer, so generally we will rewrite the Get method
// Override Get Method -(Nsarray *) dataarray{ if (nil = = _dataarray) {= [Nsarray Array]; } return _dataarray}
This prevents the member property from being assigned
In summary, objective-c lazy loading, is actually called the member property of the Get method, initialize the value, and swift lazy load, is and objective-c different
Swift
//MARK Tablview's DataSource proxy methodFunc TableView (Tableview:uitableview, numberofrowsinsection section:int)int{returnSelf.dataArray.count} func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath) /c2>-uitableviewcell{//1. Get the cellLet cell =Xwshopcell.cellwithtableview (TableView)//2. Transfer ModelCell.wine =Self.dataarray[indexpath.row]//3. Callback Cell returncell}
And this sentence
return Self.dataArray.count
in the Swift
The storage property must be initialized, confirm the type, or use an optional type, in short to confirm the type, after all Swfi
T is a type-safe language, so Swift
proposed a lazy
Properties, Usage
//1. Analysis Nsarray is a closure of the return value, and this is a closed packet without parametersLazy var Dataarray:nsarray = { [] }() //2. Can also be written like this lazy var Dataarray:nsarray = {return Nsarray ()} ()//3. Loading from the plist fileLazy var dataarray:array<xwwine> ={Let Winepath= Nsbundle.mainbundle (). Pathforresource ("wine.plist", Oftype:nil)!Let winesm=Nsmutablearray (Contentsoffile:winepath); var Tmparray:array<xwwine>! = [] forTmpwinedictinchwinesm!{var wine:xwwine= Xwwine.winewithdict (tmpwinedict as!nsdictionary) tmparray.append (wine)} print ("I run it one time") returnTmparray} ()
The above code, a bit difficult to understand, if Objective-C
the previous block
or the C语言
function pointer understanding, can be considered a block of code, and then self.dataArray
the time, the code block is executed, but repeated calls, Lazy
the properties of the code block will only be called once , the lazy
modification is a storage property, and the store is a closure, I think inside, should be optimized
The difference between Swift lazy loading (lazy) and objective-c lazy loading