gpt4 book ai didi

objective-c - cocoa 问题 - NSTask 不起作用

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

NSTask 不工作;我认为这与争论有关。这是我的代码:

 - (IBAction)downloadFile:(id)sender {

// allocate our stuff :D
progressIndication = [[NSProgressIndicator alloc] init];
NSTask *downloader = [[NSTask alloc] init];

// set up the downloader task
[downloader setLaunchPath:@"/usr/bin/curl"];
[downloader setArguments:[NSArray arrayWithObject:[NSString stringWithFormat:@"-LO %@", downloadURL]]];

// go to the Desktop!
system("cd ~/Desktop");

// start progress indicator
[progressIndication startAnimation:self];

// download!
[downloader launch];

// stop the progress indicator, everything is done! :D
[progressIndication stopAnimation:self];



}

谢谢

最佳答案

您确实不需要使用 curl 来执行此操作;只需使用 NSData 即可更轻松地完成任务:

NSData *data = [NSData dataWithContentsOfURL:downloadURL];
[data writeToFile:[[NSString stringWithFormat:@"~/Desktop/%@", [downloadURL lastPathComponent]] stringByExpandingTildeInPath] atomically:YES];
<小时/>

如果您坚持需要使用 curl 来实现此目的,则必须修复代码,但由于多种原因,该代码不起作用。首先,你的论点是错误的。您应该具有以下代码:

[downloader setArguments:[NSArray arrayWithObjects:@"-L", @"-O", [downloadURL absoluteString], @"-o", [NSString stringWithFormat:@"~/Desktop/%@", [downloadURL lastPathComponent]], nil];

其次,system("cd ~/Desktop")没有意义;摆脱它。
最后,NSTask 并发运行。 [downloader launch] 开始操作,您的代码将继续。应该是:

[downloader launch];
[downloader waitUntilExit]; // block until download completes
[progressIndication stopAnimation:self];

关于objective-c - cocoa 问题 - NSTask 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4911711/

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