简体   繁体   English

如何为NSURLConnection定义NSMutableData实例?

[英]How do I define NSMutableData instance for NSURLConnection?

From https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE 来自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.
}

I'm a relatively new iOS6 programmer. 我是一个相对较新的iOS6程序员。 First off, I think that with ARC it should be just receivedData = [NSMutableData data] ? 首先,我认为使用ARC应该只是receivedData = [NSMutableData data]

Secondly, how should I declare the receivedData instance variable? 其次,我应该如何声明receivedData实例变量? I'm guessing @property (strong, nonatomic) NSMutableData *receivedData; 我猜@property (strong, nonatomic) NSMutableData *receivedData; in the header and @synthesize receivedData in the implementation. 在标头中,@ @synthesize receivedData在实现中。

However, I'm still trying to grok multithreading and ARC in iOS6. 但是,我仍在尝试在iOS6中使用多线程和ARC。 Should the property declaration be 财产声明是否应

@property (strong, nonatomic) NSMutableData *receivedData;

or just 要不就

@property (strong) NSMutableData *receivedData;

for the received data in the delegate of an asynchronous NSURLConnection? 在异步NSURLConnection的委托中接收数据?

You should implement the remain methods of the NSURLConnectionDelegate . 您应该实现NSURLConnectionDelegate方法。 That's where you will get the data. 那就是您获取数据的地方。 If you want to use blocks, for instance, you can use this . 例如,如果要使用块,则可以使用this Atomic guarantees that even if multiple threads are accessing the ivar and changing it, a value will be always be there. Atomic保证即使多个线程正在访问ivar并对其进行更改,一个值也将始终存在。 You get some boost if you have it as nonatomic . 如果您将其视为nonatomic则会有所nonatomic Your application logic should be responsible for data integrity and not how a setter/getter are synthesised. 您的应用程序逻辑应负责数据的完整性,而不是负责setter / getter的综合方式。 So in general, should be nonatomic . 因此,一般来说,应该是非nonatomic

First off, I think that with ARC it should be just receivedData = [NSMutableData data] ? 首先,我认为使用ARC应该只是receiveData = [NSMutableData数据]?

Yes, its enough. 是的,足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM