IOS Development--Practical technology Swift article &swift lazy loading (lazy)

Source: Internet
Author: User

Swift lazy Loading (lazy) in programming, we often use * Lazy Loading *, as the name implies, is used to open up space, such as the most commonly used controls in iOS development UITableView,When we implement the data source method, we usually write like this objective-c
1 //data Source proxy methods that must be implemented2-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section3 {4     returnSelf.dataArray.count;5 }6 7-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath8 {9     //1. Get the cellTenXwshopcell *cell =[Xwshopcell Cellwithtableview:tableview]; One  A     //2. Transfer Model -Cell.wine =Self.dataarray[indexpath.row]; -  the     //3. Callback Cell -     returncell; -}

In the code above,

 1return self.dataArray.count;

is actually using

 1 @property (nonatomic, strong) Nsarray *dataarray;

@property attribute, a get and set method is generated for the property, and here is the Get method of the call, but return self.dataArray.count in the preceding code calls

-(Nsarray *) dataarray{    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

1 // overriding the Get method 2 -(Nsarray *) DataArray3{4     if (nil = = _dataarray) {  5         _dataarray = [Nsarray array]; 6     }7     return  _dataarray8 }

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
1 //MARK Tablview's DataSource proxy method2Func TableView (Tableview:uitableview, numberofrowsinsection section:int)int{3         returnSelf.dataArray.count4     }5 6Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)uitableviewcell{7         //1. Get the cell8Let cell =Xwshopcell.cellwithtableview (TableView)9 Ten         //2. Transfer Model OneCell.wine =Self.dataarray[indexpath.row] A  -         //3. Callback Cell -         returnCell the}

    • And this sentence
        1 return self.dataArray.count 

    • In the Swift storage property must initialize, confirm the type, or use optional type, in short to confirm the type, after all, Swfit is a type-safe language, so Swift proposes the lazy attribute, the usage
1   // 1. Analysis Nsarray is a closure of the return value, and this is a closed packet without parameters 2     Lazy var Dataarray:nsarray = {3         return  []4    } () 5 6     // 2. It can also be written like this 7     Lazy var Dataarray:nsarray = {8         return  nsarray ()9     }()

1     //3. Loading from the plist file2Lazy var dataarray:array<xwwine> = {3Let Winepath = Nsbundle.mainbundle (). Pathforresource ("wine.plist", Oftype:nil)!4 5Let Winesm =Nsmutablearray (contentsoffile:winepath);6 7var tmparray:array<xwwine>! = []8 9              forTmpwinedictinchwinesm! {Ten  Onevar wine:xwwine = xwwine.winewithdict (tmpwinedict as!nsdictionary) A  - tmparray.append (wine) -             } the  -Print"I run it one time") -  -             returnTmparray +}()

    • The above code, a bit difficult to understand, if before will objective-c block or the C language function pointer understand thoroughly, can be regarded as a block of code, and then Self.dataarray, then executes the code block, but repeated calls, the Lazy property of the code block will only called once , the lazy decoration is a storage property, and the store is a closure, I think inside, should be optimized

    • Personal humble opinion, there is a static storage space inside, when the space is not value, will execute the closure code, return value, will be placed in this storage space, a bit like the objective-c of the singleton object.

    • Welcome reprint, but please keep the original address Boyxiong (great god: Majestic)

IOS Development--Practical technology Swift article &swift lazy loading (lazy)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.