试图让数据从URL返回的数据为零的目标C

问题描述:

我currentley试图从该链接检索数据:http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=%E2%98%85%20Bayonet试图让数据从URL返回的数据为零的目标C

我想链路输出,当你去点击它的数据。

这里是我当前的代码:

NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=★%20Bayonet"]]; 

NSDictionary *test = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
NSLog(@"%@", test[@"lowest_price"]); 

但currentley它返回此错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' 

我认为这是与所涉及的URL字符串中的明星,不任何人都有这个解决方案?

+1

因为星号,你的'NSURL'是'nil'。所以'data'也是'nil',所以'JSONObjectWithData:options:error:'失败。 – Larme 2014-09-22 16:34:43

+0

调试涉及检查所有步骤的正确操作。有Xcode LLVM调试器,当然还有'NSLog()'。下次尝试第一次找到问题。此外,如果您对'NSURL URLWithString ...'使用了单独的语句(步骤),那么您可以立即发现问题。 – zaph 2014-09-22 16:38:35

+0

Safari允许使用非ASCII字符,因为它用正确的UTF替换它们,您需要做同样的事情。 – zaph 2014-09-22 16:41:20

原来的网址,在示例代码,格式不正确。

首先,这里是工作的代码:

#import <Foundation/Foundation.h> 

int main(int argc, char *argv[]) { 
    @autoreleasepool { 
     NSError *error = nil; 

     // NOTE: I pruned the original '%20' in your example -- partially escaped strings 
     // are difficult to deal with. 
     NSString *urlString = @"http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=★ Bayonet"; 

     // Now, escape the entire string 
     NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
     // Looks good 
     NSLog(@"%@", [url absoluteString]); 
     // http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=%E2%98%85%20Bayonet 

     // Run it 
     NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 
     NSDictionary *test = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
     NSLog(@"%@", test[@"lowest_price"]); 
     //129,99&#8364; 
     // Data is weird to me, but looks appropriate. 
    } 
} 

首先,你没有尝试使用它之前逃脱的字符串。 initWithString: docs国家:

This method expects URLString to contain only characters that are allowed in a properly formed URL. All other characters must be properly percent escaped. Any percent-escaped characters are interpreted using UTF-8 encoding.

所以,我们这样做。但是,示例代码中的原始字符串“部分转义”。如果你逃脱了URL,你会结束:

http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=%E2%98%85%2520Bayonet 

,而不是

http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=%E2%98%85%20Bayonet 

...你可以看到,在 '%20' 将自身得到逃到“%2520 ”。

我希望这会有所帮助。干杯。

编辑: 请检查您复制的URL是否有错误。 实际的URL是您在评论中提到的: @“http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=%E2%98%85%20Bayonet

NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=%E2%98%85%20Bayonet"]]; 
NSDictionary *test = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

NSLog(@"%@", test[@"lowest_price"]); 
+0

但是,即使在修复代码语法之后,这也不起作用。尝试一下。谁投了赞成票没有尝试。错误消息:data:(null), ***由于未捕获异常'NSInvalidArgumentException'而终止应用程序,原因:'数据参数为零' – zaph 2014-09-22 17:51:09

+0

仍然无法编译,仍然崩溃。尝试一下! – zaph 2014-09-22 19:57:32