Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
Xcode initWithContentsOfURL通常返回零_Xcode_Networking_Nsurlconnection_Initwithcontentsofurl - Fatal编程技术网

Xcode initWithContentsOfURL通常返回零

Xcode initWithContentsOfURL通常返回零,xcode,networking,nsurlconnection,initwithcontentsofurl,Xcode,Networking,Nsurlconnection,Initwithcontentsofurl,当我在我的iPhone上测试它时,它总是在我打开wifi时工作。然而,当我使用3G时,我经常得到零。如果我连续尝试15次(我有一个更新按钮),我最终会得到想要的结果 我的问题是,这个问题是在服务器端还是我的代码不稳定?我是否应该使用不同的方法来获取更安全的数据?您提供的信息不足以给出模糊的答案,但您确实有一些选择 最重要的是,您有一个“error”参数,您应该打印结果。还有一个稍微好一点的API可以在NSString类中使用 将代码更改为以下内容: NSError *error; NSStrin

当我在我的iPhone上测试它时,它总是在我打开wifi时工作。然而,当我使用3G时,我经常得到零。如果我连续尝试15次(我有一个更新按钮),我最终会得到想要的结果


我的问题是,这个问题是在服务器端还是我的代码不稳定?我是否应该使用不同的方法来获取更安全的数据?

您提供的信息不足以给出模糊的答案,但您确实有一些选择

最重要的是,您有一个“
error
”参数,您应该打印结果。还有一个稍微好一点的API可以在NSString类中使用

将代码更改为以下内容:

NSError *error;
NSString *string = [[NSString alloc]
                    initWithContentsOfURL:URL
                    encoding:NSUTF8StringEncoding
                    error:&error];

我现在用的是。我非常推荐这个库,它易于使用,但功能强大。

如果我不够清楚,很抱歉,但它实际上始终是UTF8。我会试着看看这是否会使过程更加稳定。虽然我真的想知道这是否可能是服务器端的问题?
NSError *error = NULL;
NSStringEncoding actualEncoding;

// variable names in Objective-C should usually start with lower case letters, so change
// URL in your code to "url", or even something more descriptive, like "urlOfOurString"
NSString *string = [[NSString alloc] initWithContentsOfURL:urlOfOurString usedEncoding:&actualEncoding error:&error];
if(string)
{
    NSLog( @"hey, I actually got a result of %@", string);

    if(actualEncoding != NSUTF8StringEncoding)
    {
        // I also suspect the string you're trying to load really isn't UTF8
        NSLog( @"and look at that, the actual encoding wasn't NSUTF8StringEncoding");
    }
} else {
    NSLog( @"error when trying to fetch from URL %@ - %@", [urlOfOurString absoluteString], [error localizedDescription]);
}