我正在尝试使用NSURLDownload下载网址,但它不能开始下载。在继续之前,我必须说我使用的是GNUStep。
我的项目概述如下:
MyClass.h:
@interface MyClass : Object {
}
-(void)downloadDidBegin:(NSURLDownload*)download;
-(void)downloadDidFinish:(NSURLDownload*)download;
@endMain.m
int main()
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSLog(@"creating url");
NSURL* url = [[[NSURL alloc] initWithString:@"http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/NSURLRequest_Class.pdf"] autorelease];
NSLog(@"creating url request");
NSURLRequest* url_request = [[[NSURLRequest alloc] initWithURL:url] autorelease];
NSLog(@"creating MyClass instance");
MyClass* my_class = [[MyClass alloc] init];
NSLog(@"creating url download");
NSURLDownload* url_download = [[[NSURLDownload alloc] initWithRequest:url_request
delegate:my_class] autorelease];
[pool drain];
}我在MyClass中的两个函数上都有NSLog,但它们都没有命中。我必须做什么才能开始下载?或者这是GNUStep的问题吗?
发布于 2010-02-22 22:17:02
NSURLDownload在后台下载,因此对initWithRequest:delegate:的调用会立即返回。
除非您的程序将控制传递给run循环(对于应用程序,这是自动处理的,但对于工具,必须手动完成),否则它将只执行main()函数的其余部分并终止。
此外,发送给您的委托的消息是从run循环中发送的,因此即使main()没有立即退出,您的委托仍然不会接收downloadDidBegin:或downloadDidFinish:,除非您的代码首先调用NSRunLoop的run方法之一。
将以下行添加到代码中,紧接在[pool drain];之前
[[NSRunLoop currentRunLoop] run];
有关run循环的更多信息,请查看Thread Programming Guide。
https://stackoverflow.com/questions/2309249
复制相似问题