【iOS開發-96】網路請求總結,深淺拷貝copy和mutableCopy,SDWebImage映像下載第三方架構

來源:互聯網
上載者:User

標籤:

(1)一般的網路請求資料的處理結構是這樣的

    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) {        //處理資料                //更新主線程        dispatch_async(dispatch_get_main_queue(), ^{            //賦值給資料arr=arrM        });    }];

當然,我們有時候需要緩衝一些資料到本地的話,可以這樣操作,把資料寫到沙箱的cache中,不能寫到Document中,會被蘋果商店拒絕的:

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) {        //處理資料        //方法之一,是把資料寫入沙箱,相當於離線資料(官方規定除APP之外產生的檔案都不能放在Document中,需放在cache中)        NSString *cache=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];        NSString *path=[cache stringByAppendingPathComponent:@"friends.plist"];        [data writeToFile:path atomically:YES];        //然後處理資料        NSArray *dictArr=[NSArray arrayWithContentsOfFile:path];        NSMutableArray *arrM=[NSMutableArray array];        for (NSDictionary *dict in dictArr) {            //字典轉模型的語句            [arrM addObject:nil];        }        //更新主線程        dispatch_async(dispatch_get_main_queue(), ^{            //賦值給資料arr=arrM        });    }];

(2)copy和mutableCopy總結

——mutableCopy是深拷貝,一聽名字就恨厲害的樣子,可以把它想象成不管被複製的對象是可變還是不可變,mutableCopy深拷貝之後都會產生一個新對象當然地址也會有所不同。

——copy是淺拷貝,copy性格比較複雜,它喜歡看人下菜,如果被拷貝的對象是不可變的,那麼拷貝後地址不變,對象還是那個唯一的對象;如果被拷貝對象是可變的,那麼它就和深拷貝一樣了,直接建立了個對象,當然地址也不一樣。這是因為經過copy拷貝後得到的變數都是imutable的,所以可以說copy時如果原變數不可變,那麼它覺得被自己copy後的變數也是不可變的,那麼十分安全,於是就直接淺拷貝了一下,地址還是相同的引用同一個對象(反正都不能修改),而如果原變數是可變的,那麼經過copy之後的變數是不可變的,所以一個可變一個不可變如果還用相同地址引用同一個對象顯然很危險,所以必須是不同地址的不同對象。當然經過copy之後的變數,儘管被定義為Mutable的,但是它仍然是iMutable的。

-(void)test2{    NSMutableString *strM=[NSMutableString stringWithString:@"hello"];    NSMutableString *str1=[strM copy];    NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);    //地址不同}-(void)test3{    NSString *[email protected]"hello";    NSMutableString *str1=[strM copy];    NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);    //地址相同}-(void)test4{    NSString *[email protected]"hello";    NSString *str1=[strM copy];    NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);    //地址相同}-(void)test5{    NSMutableString *strM=[NSMutableString stringWithString:@"hello"];    NSString *str1=[strM mutableCopy];    NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);    //地址不同}-(void)test6{    NSMutableString *strM=[NSMutableString stringWithString:@"hello"];    NSMutableString *str1=[strM mutableCopy];    NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);    //地址不同}-(void)test7{    NSString *[email protected]"hello";    NSMutableString *str1=[strM mutableCopy];    NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);    //地址不同}-(void)test8{    NSString *[email protected]"hello";    NSString *str1=[strM mutableCopy];    NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);    //地址不同}-(void)test9{    NSMutableString *strM=[NSMutableString stringWithString:@"hello"];    NSString *str1=[strM copy];    NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);    //地址不同}

(3)下載映像

——比如頭像,最好是複製給模型裡面的變數,這樣改變的是模型,如video模型

-(void)loadImg:(NSIndexPath *)indexPath{    //取得對應模型    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];        //重新整理表格(局部重新整理)        dispatch_async(dispatch_get_main_queue(), ^{            [self.tableView reloadRowsAtIndexPath:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];        });    }];}//然後在tableView的資料方法cell的賦值方法中賦值即可,if(!v.image){    cell.imageView.image=[UIImage imageNamed:@"default"];}else{    cell.imageView.image=v.image;}

(4)第三方架構來進行映像下載:SDWebImage,:https://github.com/rs/SDWebImage/

——匯入第三方架構後,第一件事情就是CMD+B進行編譯,以防部分架構需要依賴其他架構才能運行。一般編譯都會出現很多警告,正常。

——大部分人都只用了UIImageView+WebCache這個分類來處理圖片

以上(3)中的代碼可以修改成,直接在cell賦值方法中,這個方法就是架構提供的方法:

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;//記得給模型賦值    }];}


備忘:

(1)繼承的子類中,如果需要使用成員變數(_name之類的),需要合成一下,@synthesize name=_name;


【iOS開發-96】網路請求總結,深淺拷貝copy和mutableCopy,SDWebImage映像下載第三方架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.