zoukankan      html  css  js  c++  java
  • 记录群聊天关于block内部引用问题

    // %p &testStr 是object自身的内存地址
    
    // %p testStr  是指向的内存地址 
    
    // block本质是对象,访问外部变量当成自己的成员变量
    
    // block内有一个新的引用,指向原对象所指向的内存地址

    对于对象:

    @autoreleasepool {
            NSMutableString *testStr = [[[NSMutableString alloc]initWithString:@"String"] autorelease];
            NSLog(@" NORMAL : %@, %p, %p", testStr, testStr, &testStr);
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                [testStr appendString:@" Append"];
                NSLog(@"Bgroud : %@, %p, %p", testStr, testStr, &testStr);
            });
            NSLog(@" Append : %@, %p, %p", testStr, testStr, &testStr);
        }

    打印结果:

    2014-05-23 09:38:13.772 PPPTest[1839:60b]  NORMAL : String, 0x8e74cd0, 0xbfffc8c4
    2014-05-23 09:38:13.772 PPPTest[1839:60b]  Append : String, 0x8e74cd0, 0xbfffc8c4
    2014-05-23 09:38:13.772 PPPTest[1839:1303] Bgroud : String Append, 0x8e74cd0, 0x8e74e94

    对于基本类型:

    @autoreleasepool {
            __block int nInt = 10;
            NSLog(@"    : %d, %p", nInt, &nInt);
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                ++nInt;
                NSLog(@"bg : %d, %p", nInt, &nInt);
            });
            ++nInt;
            NSLog(@" ++ : %d, %p", nInt, &nInt);
        }

    打印结果:

    2014-05-23 09:38:54.446 PPPTest[1851:60b]     : 10, 0xbfffc8c8
    2014-05-23 09:38:54.446 PPPTest[1851:60b]  ++ : 11, 0x8d7be50
    2014-05-23 09:38:54.446 PPPTest[1851:1303] bg : 12, 0x8d7be50
  • 相关阅读:
    durex-word
    闲聊可穿戴设备
    闲聊质数
    一步一步学swift之:自己写Api接口-PHP
    Swift实战-小QQ(第2章):QQ侧滑菜单
    Swift实战-小QQ(第1章):QQ登录界面
    一步一步学习Swift之(一):关于swift与开发环境配置
    objective-c底层: runtime机制
    手把手教你视频直播开发
    多语言本地化开发Localized
  • 原文地址:https://www.cnblogs.com/tianglin/p/3747001.html
Copyright © 2011-2022 走看看