Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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
Ios 程序跳过用于下载txt文件的NSURLRequest,如果与其他代码一起放置在IB操作中_Ios_Objective C_Curl_Nsurl - Fatal编程技术网

Ios 程序跳过用于下载txt文件的NSURLRequest,如果与其他代码一起放置在IB操作中

Ios 程序跳过用于下载txt文件的NSURLRequest,如果与其他代码一起放置在IB操作中,ios,objective-c,curl,nsurl,Ios,Objective C,Curl,Nsurl,我使用objective-c中的NSURL请求下载网页并将其放入文本文件中。这类似于UNIX中使用的curl函数 代码如下: NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *filePath = [documentDir stringByAppendingPathCompo

我使用objective-c中的NSURL请求下载网页并将其放入文本文件中。这类似于UNIX中使用的curl函数

代码如下:

    NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [documentDir stringByAppendingPathComponent:@"data.txt"];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://ca.finance.yahoo.com/q?s=JNJ"]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (error) {
               NSLog(@"Download Error:%@",error.description);
        }
        if (data) {
            [data writeToFile:filePath atomically:YES];
            NSLog(@"File is saved to %@",filePath);
        }
    }];
我发现,如果我在自己的IB操作中执行代码,没有其他代码链接到测试按钮,那么它就没有bug

然而,在主IB操作中,程序编译器似乎跳过了代码,没有执行它,因为缺少下载的文件。如果您能提供有关此错误可能来源的反馈,我将不胜感激。 谢谢

您必须创建NSOperationQueue实例:

[NSURLConnection sendAsynchronousRequest:request [NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

输入一些断点,检查它们是否被击中,如果被击中,结果如何。编译器等不会跳过代码,它会完全执行您编写的代码。奇怪的是,它会命中所有断点,甚至是NSURLConnection。但是,它没有输入任何一个if语句。我添加了一个else,它也没有输入。我认为这意味着它正在执行NSURLConnection,但没有下载文件。可能是什么原因造成的?当我试图打印NSUrlResponse或将其分配给变量以检查错误代码时,请检查NSUrlResponse对象以查看是否收到HTTP错误代码Xcode表示该变量是未声明的标识符。这是检查错误代码的错误方法吗?可以,但是我需要更多的声誉才能投票,