我想从网址和JSON
解析NSLog
。这是我的网址:http://api.kivaws.org/v1/loans/search.json?status=fundraising
这是我的代码。当我运行我的代码时崩溃.....
NSURL * url=[NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];
NSData* data = [NSData dataWithContentsOfURL:url];
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data options:kNilOptions
error:&error];
NSLog(@"%@",json);
请帮帮我....这是我的崩溃错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104827f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000106905bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000104827e6d +[NSException raise:format:] + 205
3 Foundation 0x00000001065aa1bf +[NSJSONSerialization JSONObjectWithData:options:error:] + 67
4 json 0x00000001046608b9 -[ViewController viewDidLoad] + 185
5 UIKit 0x00000001052d3a90 -[UIViewController loadViewIfRequired] + 738
6 UIKit 0x00000001052d3c8e -[UIViewController view] + 27
7 UIKit 0x00000001051f2ca9 -[UIWindow addRootViewControllerViewIfPossible] + 58
8 UIKit 0x00000001051f3041 -[UIWindow _setHidden:forced:] + 247
9 UIKit 0x0000000112eac7b0 -[UIWindowAccessibility _orderFrontWithoutMakingKey] + 68
10 UIKit 0x00000001051ff72c -[UIWindow makeKeyAndVisible] + 42
11 UIKit 0x00000001051aa061 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
12 UIKit 0x00000001051acd2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
13 UIKit 0x00000001051abbf2 -[UIApplication workspaceDidEndTransaction:] + 179
14 FrontBoardServices 0x000000010b6692a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
15 CoreFoundation 0x000000010475d53c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
16 CoreFoundation 0x0000000104753285 __CFRunLoopDoBlocks + 341
17 CoreFoundation 0x0000000104753045 __CFRunLoopRun + 2389
18 CoreFoundation 0x0000000104752486 CFRunLoopRunSpecific + 470
19 UIKit 0x00000001051ab669 -[UIApplication _run] + 413
20 UIKit 0x00000001051ae420 UIApplicationMain + 1282
21 json 0x0000000104660ca3 main + 115
22 libdyld.dylib 0x0000000107089145 start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
答案 0 :(得分:-1)
如果您需要知道失败的原因,请从
开始NSError* error = nil;
NSData* data = [NSData dataWithContentsOfURL:url options: nil error:&error]:
然后,如果失败,您可以使用
查看错误NSLog@"Error description = %@", [error localizedDescription];
此外,正如其他人所说,您应该更改代码,以便异步提取URL。相反,对于非文件URL,请考虑使用NSSession类的dataTaskWithURL:completionHandler:方法。有关详细信息,请参阅URL Loading System Programming Guide。