__weak and autorelease pool in ARC in Xcode 4.2

与世无争的帅哥 提交于 2019-12-05 18:39:24
NSString __weak *string;
@autoreleasepool {
        string = [NSString stringWithFormat:@"%@", @"AAA"];
}

NSLog(@"string: %@", string);

it outputs as the following what you want.

string: (null)

Thus,

string = [NSString stringWithString:@"AAA"];

is same as

string = @"AAA";

the constant string literal that is not allocated in the heap.

EDITED:

str variable has still strong reference for the autoreleased object.

The following code is what exactly you want.

NSString __weak *string;
{
    NSString __strong *str;
    @autoreleasepool {
        str = [NSString stringWithFormat:@"%@", @"AAA" ];
        string = str;
    }
}
NSLog(@"string: %@", string);

And

NSString __weak *string;
@autoreleasepool {
    NSString __strong *str;
    str = [NSString stringWithFormat:@"%@", @"AAA" ];
    string = str;
}
NSLog(@"string: %@", string);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!