gpt4 book ai didi

cocoa - 区分 delegate 中的 NSURLConnection 对象

转载 作者:行者123 更新时间:2023-12-03 16:10:54 24 4
gpt4 key购买 nike

我有两个请求一个接一个地开始。像这样开始请求

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
NSURLConnection * connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[connection start];

另一个请求是这样开始的。

NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.apple.com"]];
NSURLRequest *request1 = [NSURLRequest requestWithURL:url1 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
NSURLConnection *connection1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
[connection1 release];

如何在委托(delegate)方法中区分这两者?

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{}

不想为此目的保留任何额外的类变量。

最佳答案

很简单:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection == connection1)
{
//It's for connection1.
}
else if (connection == connection2)
{
//It's for connection2.
}
}

您可以浏览一下这个美丽的问题:Managing multiple asynchronous NSURLConnection connections

关于cocoa - 区分 delegate 中的 NSURLConnection 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16393830/

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