Objective-C中有多个NSURLConnection委托 [英] Multiple NSURLConnection delegates in Objective-C
问题描述
我有两个NSURLConnections。第二个依赖于第一个的内容,所以处理从连接接收的数据将对于两个连接不同。
I have two NSURLConnections. The second one depends on the content of the first, so handling the data received from the connection will be different for the two connections.
我只是选择了Objective-C,我想知道实现代理的正确方法是什么。
I'm just picking up Objective-C and I would like to know what the proper way to implement the delegates is.
现在我正在使用:
NSURL *url=[NSURL URLWithString:feedURL];
NSURLRequest *urlR=[[[NSURLRequest alloc] initWithURL:url] autorelease];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:urlR delegate:self];
我不想使用self作为委托,我如何使用不同的委托定义两个连接?
I don't want to use self as the delegate, how do I define two connections with different delegates?
NSURLConnection *c1 = [[NSURLConnection alloc] initWithRequest:url delegate:handle1];
NSURLConnection *c2 = [[NSURLConnection alloc] initWithRequest:url delegate:handle2];
如何创建handle1和handle2作为实现?还是接口?我真的不知道你会怎么做。
How would do i create handle1 and handle2 as implementations? Or interfaces? I don't really get how you would do this.
任何帮助都是真棒。
谢谢,
Brian Gianforcaro
Thanks, Brian Gianforcaro
推荐答案
在您的示例中,您将分配一个DownloadDelegate对象,而不必初始化它。
In your sample, you alloc a DownloadDelegate object without ever init'ing it.
DownloadDelegate *dd = [DownloadDelegate alloc];
这很危险。而是:
DownloadDelegate *dd = [[DownloadDelegate alloc] init];
此外,并非严格响应方法在你的@interface声明(当然不会伤害,)。最后,你需要确保你实现连接:didFailWithError:和connectionDidFinishLoading:to-release你的DownloadDelegate对象,否则你会泄露。
Also, it's not strictly necessary to declare your delegate response methods in your @interface declaration (though it won't hurt, of course). Finally, you'll want to make sure that you implement connection:didFailWithError: and connectionDidFinishLoading: to -release your DownloadDelegate object, otherwise you'll leak.
重新启动并运行!
这篇关于Objective-C中有多个NSURLConnection委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!