亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS MRC 下 block 循環(huán)引用問題實例講解

 更新時間:2017年12月12日 10:37:25   作者:jeffasd  
本文通過文字說明加代碼的形式給大家介紹了iOS MRC 下 block 循環(huán)引用問題,非常不錯,具有參考借鑒價值,需要的朋友參考下吧

下面一段代碼給大家介紹iOS MRC 下 block 循環(huán)引用問題            

  //注意此__block會復(fù)制一份指針出來 一次原始的指針如果置為nil的話,此處復(fù)制出來的指針還是野指針 
    __block __typeof(self)weakSelf = self; 
    //__weak __typeof(self)weakSelf = self; 
    //__weak Person *weakSelf = self; 
    void (^block)(void) = ^(void){ 
      //NSLog(@"name --> is %@", self.name); 
      //NSLog(@"name --> is %@", weakSelf.name); 
      //這樣判斷會crash 此時weakSelf為野指針 
      //weakSelf 這時候是個野指針。。。野指針也是指針對吧?反正,這個野指針并不為NULL,雖然它指向的內(nèi)存并未有什么鳥用, 
      //然而代碼并不知道。所以 執(zhí)行[weakSelf doSomething]; 必然閃退。 
      //注意此__block會復(fù)制一份指針出來 一次原始的指針如果置為nil的話,此處復(fù)制出來的指針還是野指針 
//      if (weakSelf) { 
//        NSLog(@"name --> is %@", weakSelf.name); 
//      } 
      //malloc(22); 
//      malloc_zon 
      //這并沒有什么卵用。。。weakSelf 已經(jīng)是野指針 照樣crash 
//      __strong __typeof(weakSelf) strongSelf = weakSelf; 
//      if (weakSelf) { 
//        NSLog(@"name --> is %@", strongSelf.name); 
//      } 
      if (malloc_zone_from_ptr(weakSelf)) { 
        NSLog(@"name --> is %@", weakSelf.name); 
      } 
// 
// ViewController.m 
// test_mrc_block_self_01 
// 
// Created by jeffasd on 2017/12/1. 
// Copyright © 2017年 jeffasd. All rights reserved. 
// 
#import "ViewController.h" 
#import "Person.h" 
@interface ViewController () 
@property (nonatomic, copy) NSString *name; 
@end 
@implementation ViewController 
- (void)viewDidLoad { 
  [super viewDidLoad]; 
  self.view.backgroundColor = [UIColor whiteColor]; 
  self.name = @"xiaoming"; 
} 
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 
  self.view.backgroundColor = [UIColor cyanColor]; 
//  void (^block)(void) = ^(void){ 
//    NSLog(@"name --> is %@", self.name); 
//  }; 
//   
//   
//   
//  for (int i = 0; i < 30; i++) { 
//    block(); 
//  } 
  Person *xiaoming = [[Person alloc] init]; 
  //[xiaoming retain]; 
  [xiaoming release]; 
//  xiaoming = nil; 
  xiaoming = NULL; 
} 
- (void)didReceiveMemoryWarning { 
  [super didReceiveMemoryWarning]; 
  // Dispose of any resources that can be recreated. 
} 
@end 
// 
// Person.m 
// test_mrc_block_self_01 
// 
// Created by jeffasd on 2017/12/1. 
// Copyright © 2017年 jeffasd. All rights reserved. 
// 
#import "Person.h" 
#include <malloc/malloc.h> 
@interface Person () 
@property (nonatomic, copy) NSString *name; 
@end 
@implementation Person 
- (instancetype)init{ 
  if (self = [super init]) { 
    self.name = @"xiaoming"; 
    //注意此__block會復(fù)制一份指針出來 一次原始的指針如果置為nil的話,此處復(fù)制出來的指針還是野指針 
    __block __typeof(self)weakSelf = self; 
    //__weak __typeof(self)weakSelf = self; 
    //__weak Person *weakSelf = self; 
    void (^block)(void) = ^(void){ 
      //NSLog(@"name --> is %@", self.name); 
      //NSLog(@"name --> is %@", weakSelf.name); 
      //這樣判斷會crash 此時weakSelf為野指針 
      //weakSelf 這時候是個野指針。。。野指針也是指針對吧?反正,這個野指針并不為NULL,雖然它指向的內(nèi)存并未有什么鳥用, 
      //然而代碼并不知道。所以 執(zhí)行[weakSelf doSomething]; 必然閃退。 
      //注意此__block會復(fù)制一份指針出來 一次原始的指針如果置為nil的話,此處復(fù)制出來的指針還是野指針 
//      if (weakSelf) { 
//        NSLog(@"name --> is %@", weakSelf.name); 
//      } 
      //malloc(22); 
//      malloc_zon 
      //這并沒有什么卵用。。。weakSelf 已經(jīng)是野指針 照樣crash 
//      __strong __typeof(weakSelf) strongSelf = weakSelf; 
//      if (weakSelf) { 
//        NSLog(@"name --> is %@", strongSelf.name); 
//      } 
      if (malloc_zone_from_ptr(weakSelf)) { 
        NSLog(@"name --> is %@", weakSelf.name); 
      } 
    }; 
    for (int i = 0; i < 300; i++) { 
//      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
//        block(); 
//      }); 
      dispatch_async(dispatch_get_main_queue(), ^{ 
        block(); 
      }); 
    } 
  } 
  return self; 
} 
@end 

總結(jié)

以上所述是小編給大家介紹的iOS MRC 下 block 循環(huán)引用問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • iOS利用Label實現(xiàn)的簡單高性能標(biāo)簽TagView

    iOS利用Label實現(xiàn)的簡單高性能標(biāo)簽TagView

    這篇文章主要給大家介紹了關(guān)于iOS利用Label實現(xiàn)的簡單高性能標(biāo)簽TagView的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • 深入詳解Objective-C中的@Synchronized關(guān)鍵字

    深入詳解Objective-C中的@Synchronized關(guān)鍵字

    這篇文章主要為大家介紹了深入詳解Objective-C中的@Synchronized關(guān)鍵字,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Unity iOS混合開發(fā)界面切換思路解析

    Unity iOS混合開發(fā)界面切換思路解析

    這篇文章主要介紹了Unity iOS混合開發(fā)界面切換思路解析的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • IOS 創(chuàng)建并發(fā)線程的實例詳解

    IOS 創(chuàng)建并發(fā)線程的實例詳解

    這篇文章主要介紹了IOS 創(chuàng)建并發(fā)線程的實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 詳解IOS UITableViewCell 的 imageView大小更改

    詳解IOS UITableViewCell 的 imageView大小更改

    這篇文章主要介紹了詳解IOS UITableViewCell 的 imageView大小更改的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • iOS開發(fā)中指紋識別簡單介紹

    iOS開發(fā)中指紋識別簡單介紹

    指紋識別是在iOS8.0以后才推出的,所以我們?nèi)绻氚阎讣y集成到我們的APP當(dāng)中,我們首先就要在代碼中判斷iOS版本。接下來通過本文給大家分享iOS開發(fā)中指紋識別簡單介紹,需要的朋友參考下吧
    2017-11-11
  • iOS多媒體音頻(下)-錄音及其播放的實例

    iOS多媒體音頻(下)-錄音及其播放的實例

    本篇文章主要介紹了iOS多媒體音頻(下)-錄音及其播放的實例,非常具有實用價值,需要的朋友可以參考下。
    2016-12-12
  • iOS10添加本地推送(Local Notification)實例

    iOS10添加本地推送(Local Notification)實例

    這篇文章主要為大家詳細(xì)介紹了iOS10添加本地推送(Local Notification)實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • iOS如何獲取手機的Mac地址

    iOS如何獲取手機的Mac地址

    這篇文章主要為大家詳細(xì)介紹了iOS獲取手機的Mac地址的多種方法,感興趣的小伙伴們可以參考一下
    2016-04-04
  • iOS開發(fā)第三方鍵盤處理實例代碼

    iOS開發(fā)第三方鍵盤處理實例代碼

    本篇文章主要介紹了iOS開發(fā)第三方鍵盤處理實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06

最新評論