objective-c - ARC 禁止发送 'retain' 问题的显式消息

标签 objective-c xcode cocoa memory-management 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.
}

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

如何处理?我正在使用 Xcode 4.4.1

最佳答案

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

单击左侧导航 View 中的项目名称,转到 Targets -> Build Phases 并将 -fno-objc-arc 添加到任何相关的“compiler flags”文件。

See here for info on removing.

See here for basic info on ARC.

关于objective-c - ARC 禁止发送 'retain' 问题的显式消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11877025/

相关文章:

c++ - Xcode 调试器找不到我的文件

Xcode:iOS Simulator Dev Menu 无法打开

xcode - cocoa - 应用程序架构

cocoa - 是否可以使 CFBundleTypeIconFile 动态化?

iphone - 如何在 Objective C 中正确初始化子类?

objective-c - NSMutableArray 计数方法显示 NSMutableArray 计数为 0?

iphone - 何时调用 obtainPermanentIDsForObjects :?

macos - 沙盒应用程序中的 AppleScript `activate` 不会将窗口带到前台

objective-c - 使用 Xcode 和 Interface Builder 创建原型(prototype) View

iphone - 这个 Objective-C 嵌套 NSArray 会导致 iPhone 上的内存泄漏吗?