【问题标题】:How do I define NSMutableData instance for NSURLConnection?如何为 NSURLConnection 定义 NSMutableData 实例?
【发布时间】:2012-10-20 23:05:16
【问题描述】:

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

【问题讨论】:

    标签: ios ios6 nsurlconnection nsmutabledata nsurlconnectiondelegate


    【解决方案1】:

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

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

    是的,够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多