ARC禁止明确发送"保留"问题的消息

Kir*_*Kir 12 xcode cocoa memory-management objective-c automatic-ref-counting

我正在使用Apple指南中的这个非常简单的代码:

NSMutableData *receivedData;

// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                    timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    receivedData = [[NSMutableData data] retain];
} else {
    // Inform the user that the connection failed.
}
Run Code Online (Sandbox Code Playgroud)

但是对于receivedData = [[NSMutableData data] retain];Xcode 行我给出了一个错误:PushController.m:72:25: ARC forbids explicit message send of 'retain'

怎么处理呢?我正在使用Xcode 4.4.1

MrH*_*hma 44

您目前正在使用ARC为您引用计数.(ARC是"自动引用计数",iOS 5的一项新功能).因此,您无需手动保留或释放.您可以一起删除保留呼叫,也可以通过执行以下操作关闭ARC:

单击左侧导航视图中项目的名称,转到Targets - > Build Phases并添加-fno-objc-arc到任何相关文件的"编译器标志".

有关删除的信息,请参阅此处

有关ARC的基本信息,请参见此处.

  • -fno-objc-arc我将这个解决方案作为初学者的老式补习. (2认同)

归档时间:

查看次数:

32027 次

最近记录:

9 年,5 月 前