gpt4 book ai didi

ios - 如何为 NSURLConnection 定义 NSMutableData 实例?

转载 作者:行者123 更新时间:2023-11-29 04:21:02 25 4
gpt4 key购买 nike

来自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 实例变量?我猜测 header 中的 @property (strong, nonatomic) NSMutableData *receivedData; 和实现中的 @synthesize receiveData

但是,我仍在尝试在 iOS6 中摸索多线程和 ARC。属性(property)申报是否应为

@property (strong, nonatomic) NSMutableData *receivedData;

或者只是

@property (strong) NSMutableData *receivedData;

异步 ​​NSURLConnection 委托(delegate)中接收到的数据?

最佳答案

您应该实现 NSURLConnectionDelegate 的其余方法。您将在此处获取数据。例如,如果您想使用 block ,您可以使用 this . Atomic 保证即使多个线程访问 ivar 并更改它,值也将始终存在。如果您将其设置为非原子,您会得到一些提升。您的应用程序逻辑应该负责数据完整性,而不是如何合成 setter/getter。所以一般来说,应该是非原子

First off, I think that with ARC it should be just receivedData = [NSMutableData data] ?

是的,够了。

关于ios - 如何为 NSURLConnection 定义 NSMutableData 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12993461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com