0

来自https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE

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.
}

我是一个相对较新的 iOS6 程序员。首先,我认为ARC应该只是receivedData = [NSMutableData data]

其次,我应该如何声明receivedData实例变量?我猜@property (strong, nonatomic) NSMutableData *receivedData;在标题和@synthesize receivedData实现中。

但是,我仍在尝试在 iOS6 中探索多线程和 ARC。财产声明应该是

@property (strong, nonatomic) NSMutableData *receivedData;

要不就

@property (strong) NSMutableData *receivedData;

在异步 NSURLConnection 的委托中接收到的数据?

4

1 回答 1

1

您应该实现NSURLConnectionDelegate. 这就是您将获得数据的地方。例如,如果你想使用块,你可以使用thisAtomic保证即使多个线程正在访问 ivar 并对其进行更改,值也将始终存在。如果你有它,你会得到一些提升nonatomic。您的应用程序逻辑应该负责数据完整性,而不是 setter/getter 的合成方式。所以一般来说,应该是nonatomic

首先,我认为使用 ARC 应该只是 receivedData = [NSMutableData data] ?

是的,够了。

于 2012-10-20T23:21:36.263 回答