"iOS Dev-96" Web request summary, depth copy copy and mutablecopy,sdwebimage image download third-party framework

Source: Internet
Author: User
Tags shallow copy

(1) The processing structure of the general network request data is like this

    Nsurl *url=[nsurl Urlwithstring:nil];    Nsurlrequest *request=[nsurlrequest requestwithurl:url cachepolicy:0 timeoutinterval:5.0f];    [Nsurlconnection sendasynchronousrequest:request queue:[[nsoperationqueue Alloc]init] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {        //Process data                //update main thread        Dispatch_async ( Dispatch_get_main_queue (), ^{            //assignment to Data arr=arrm        });    

Of course, sometimes we need to cache some data locally, so we can do this, write the data into the sandbox cache, not write to the document, will be rejected by the Apple Store:

Nsurl *url=[nsurl Urlwithstring:nil];    Nsurlrequest *request=[nsurlrequest requestwithurl:url cachepolicy:0 timeoutinterval:5.0f]; [Nsurlconnection sendasynchronousrequest:request queue:[[nsoperationqueue Alloc]init] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {//Processing of data//method is to write data to sandbox, equivalent to offline data (Official Rules except app The resulting files cannot be placed in the document and need to be placed in the cache) NSString *cache=[nssearchpathfordirectoriesindomains (Nscachesdirectory,        Nsuserdomainmask, YES) lastobject];        NSString *path=[cache stringbyappendingpathcomponent:@ "Friends.plist"];        [Data Writetofile:path Atomically:yes];        Then processing data nsarray *dictarr=[nsarray Arraywithcontentsoffile:path];        Nsmutablearray *arrm=[nsmutablearray Array];        For (Nsdictionary *dict in Dictarr) {//dictionary to model statements [ARRM Addobject:nil];    }//Update main thread Dispatch_async (Dispatch_get_main_queue (), ^{//assignment to Data arr=arrm}); }];

(2) Copy and mutablecopy summary

--mutablecopy is a deep copy, a name to hate the appearance of a strong, you can think of it as whether the copied object is variable or immutable, Mutablecopy deep copy will produce a new object of course the address will be different.

--copy is a shallow copy, copy character is more complex, it likes due, if the copy of the object is immutable, then the copy address is unchanged, the object is the only object, if the copied object is mutable, then it is the same as the deep copy, directly new objects, of course, the address is not the same. This is because after copy copy of the variables are imutable, so it can be said that copy when the original variable is immutable, then it feels that the variables after their copy is also immutable, so very safe, so directly shallow copy a bit, The address is the same as the same object (which cannot be modified anyway), and if the original variable is mutable, the variable after the copy is immutable, so a mutable one is immutable if it is also clearly dangerous to reference the same object with the same address, it must be a different object of different addresses. Of course, after the copy of the variable, although defined as mutable, but it is still imutable.

-(void) test2{nsmutablestring *strm=[nsmutablestring stringwithstring:@ "Hello"];    nsmutablestring *STR1=[STRM Copy];    NSLog (@ "%@,%@,%p,%p", STRM,STR1,STRM,STR1);    Address different}-(void) test3{nsstring *[email protected] "Hello";    nsmutablestring *STR1=[STRM Copy];    NSLog (@ "%@,%@,%p,%p", STRM,STR1,STRM,STR1);    Address same}-(void) test4{nsstring *[email protected] "Hello";    NSString *STR1=[STRM Copy];    NSLog (@ "%@,%@,%p,%p", STRM,STR1,STRM,STR1);    Address same}-(void) test5{nsmutablestring *strm=[nsmutablestring stringwithstring:@ "Hello"];    NSString *STR1=[STRM Mutablecopy];    NSLog (@ "%@,%@,%p,%p", STRM,STR1,STRM,STR1);    Address different}-(void) test6{nsmutablestring *strm=[nsmutablestring stringwithstring:@ "Hello"];    Nsmutablestring *STR1=[STRM Mutablecopy];    NSLog (@ "%@,%@,%p,%p", STRM,STR1,STRM,STR1);    Address different}-(void) test7{nsstring *[email protected] "Hello";    Nsmutablestring *STR1=[STRM Mutablecopy];    NSLog (@ "%@,%@,%p,%p", STRM,STR1,STRM,STR1); Address different}-(vOID) test8{nsstring *[email protected] "Hello";    NSString *STR1=[STRM Mutablecopy];    NSLog (@ "%@,%@,%p,%p", STRM,STR1,STRM,STR1);    Address different}-(void) test9{nsmutablestring *strm=[nsmutablestring stringwithstring:@ "Hello"];    NSString *STR1=[STRM Copy];    NSLog (@ "%@,%@,%p,%p", STRM,STR1,STRM,STR1); Different address}

(3) Download image

such as avatars, it is best to copy the variables inside the model, so that the model is changed, such as the video model

-(void) Loadimg: (Nsindexpath *) indexpath{    //Get corresponding model    Video *v=self.videolist[indexpath.row]    [ Nsurlconnection sendasynchronousrequest:nil queue:[[nsoperationqueue Alloc]init] completionHandler:^ (NSURLResponse *response, NSData *data, Nserror *connectionerror) {        v.image=[uiimage imagewithdata:data];        Refresh table (local refresh)        Dispatch_async (Dispatch_get_main_queue (), ^{            [Self.tableview reloadrowsatindexpath:@[ Indexpath] withrowanimation:uitableviewrowanimationnone];});}    ];} The TableView is then assigned a value in the assignment method of the data method cell, if (!v.image) {    cell.imageview.image=[uiimage imagenamed:@ "Default"];} else{    Cell.imageview.image=v.image;}

(4) Third-party framework for image download: sdwebimage,:https://github.com/rs/sdwebimage/

--After importing a third-party framework, the first thing to do is compile cmd+b to prevent some frameworks from relying on other frameworks to run. General compilation will appear a lot of warnings, normal.

--Most people use the Uiimageview+webcache category to process pictures.

The code in the above (3) can be modified directly in the cell assignment method, which is the method provided by the framework:

if (v.image) {    cell.imageview.image=v.image;} else{    [Cell.imageview setimagewithurl:url placeholderImage:self.placeHolderImg options:0 completed:^ (UIImage * Image,nserror *error,sdimagecachetype cachetype) {        v.image=image;//Remember to assign a value to the model    }];}


Note:

(1) in the subclass of inheritance, if you need to use member variables (such as _name), you need to synthesize a bit, @synthesize name=_name;


"iOS Dev-96" Web request summary, depth copy copy and mutablecopy,sdwebimage image download third-party framework

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.