Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSURL下载未启动_Objective C_Cocoa_Gnustep - Fatal编程技术网

Objective c NSURL下载未启动

Objective c NSURL下载未启动,objective-c,cocoa,gnustep,Objective C,Cocoa,Gnustep,我正在尝试使用NSURLDownload下载URL,但它没有开始下载。在继续之前,必须说明我正在为此使用GNUStep 我的项目大纲如下: MyClass.h: @interface MyClass : Object { } -(void)downloadDidBegin:(NSURLDownload*)download; -(void)downloadDidFinish:(NSURLDownload*)download; @end Main.m int main() { NSAu

我正在尝试使用NSURLDownload下载URL,但它没有开始下载。在继续之前,必须说明我正在为此使用GNUStep

我的项目大纲如下:

MyClass.h:

@interface MyClass : Object {

}

-(void)downloadDidBegin:(NSURLDownload*)download;
-(void)downloadDidFinish:(NSURLDownload*)download;

@end
Main.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的问题?

NSURLDownload在后台下载,因此对
initWithRequest:delegate:
的调用立即返回

除非您的程序将控制传递给运行循环(对于应用程序,这是自动处理的,但是对于工具,必须手动完成),否则它将只执行main()函数的其余部分并终止

此外,发送给您的代理的消息是从运行循环中发送的,因此,即使main()没有立即退出,您的代理仍然不会收到
downloadDidBegin:
downloadDidFinish:
,除非您的代码首先调用了
NSRunLoop
的一个运行方法

[pool drain]前面的代码中添加以下行

[[nsrunlop currentRunLoop]run]


有关运行循环的更多信息,请查看。

您确定要对对象进行子类化而不是NSObject吗?您可能应该使用NSObject,以防有什么东西将您的委托放入集合或其他东西中。(另外,您确实应该考虑一个比“MyClass”更好的名称,并通过遵循内存管理规则适当地管理对象的生存期:)