如何为NSURLConnection定义NSMutableData实例?

时间:2012-10-20 23:05:17

标签: ios ios6 nsurlconnection nsmutabledata nsurlconnectiondelegate

来自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的委托中接收到数据?

1 个答案:

答案 0 :(得分:1)

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

  

首先,我认为使用ARC它应该只是receiveData =   [NSMutableData数据]?

是的,够了。