JSON:由于未捕获的异常 'NSInvalidArgumentException',正在终止应用程序,原因:'data parameter is nil'
JSON: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
我使用 kimono 创建了自己的 api,我正尝试使用此 api 来解析 json 数据。这是我 运行 应用程序时发生的错误。我很确定 url 地址或 api 键没有拼写错误,因为我从 kimono 复制并粘贴了它们。有什么建议吗?
提前致谢。
#import "PhotoGalleryTableViewController.h"
#define API_KEY @"xgp4nU6xA9UcBWSe0MIHcBVbAWz5v4wR"
@interface PhotoGalleryTableViewController ()
{
NSMutableArray *myObject;
NSDictionary *dictionary;
NSString *description;
NSString *photo;
}
@end
@implementation PhotoGalleryTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
photo = @"photo";
description = @"description";
myObject = [[NSMutableArray alloc] init];
NSString *urlAsString = [ NSString stringWithFormat:@"https://www.kimonolabs.com/api/2v35aqn0?apikey=xgp4nU6xA9UcBWSe0MIHcBVbAWz5v4wR"];
NSData *jsonSource = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlAsString]];
NSLog(@"%@", jsonSource);
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource
options:NSJSONReadingMutableContainers
error:nil];
for(NSDictionary *dataDict in jsonObjects) {
NSString *photo_data = [dataDict objectForKey:@"photo"];
NSString *description_data = [dataDict objectForKey:@"description"];
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
photo_data, photo,
description_data, description,
nil];
[myObject addObject:dictionary];
}
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
-( NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return myObject.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier= @"item";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
NSMutableString *text= [NSString stringWithFormat:@"%@",
[tmpDict objectForKeyedSubscript:description]];
NSMutableString *images;
images = [NSMutableString stringWithFormat:@"%@",
[tmpDict objectForKey:photo]];
NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:photo]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.textLabel.text = text;
cell.imageView.frame = CGRectMake(0, 0, 80, 70);
cell.imageView.image = img;
// Configure the cell...
return cell;
}
@end
堆栈跟踪:
stacktrace : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fdb6b65a890'
*** First throw call stack:
(
0 CoreFoundation 0x000000010fe9ff35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010fb38bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010fea704d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010fdff27c ___forwarding___ + 988
4 CoreFoundation 0x000000010fdfee18 _CF_forwarding_prep_0 + 120
5 MobileChallenge 0x000000010f5e40d1 -[PhotoGalleryTableViewController viewDidLoad] + 817
6 UIKit 0x00000001105f4a90 -[UIViewController loadViewIfRequired] + 738
7 UIKit 0x00000001105f4c8e -[UIViewController view] + 27
8 UIKit 0x0000000110b9641e -[_UIFullscreenPresentationController _setPresentedViewController:] + 65
9 UIKit 0x00000001105d0429 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 105
10 UIKit 0x0000000110600a41 -[UIViewController _presentViewController:withAnimationController:completion:] + 1746
11 UIKit 0x0000000110602d81 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 132
12 UIKit 0x0000000110602ca5 -[UIViewController presentViewController:animated:completion:] + 229
13 UIKit 0x00000001105b7360 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1242
14 UIKit 0x00000001105b74d4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
15 UIKit 0x00000001104f2331 _applyBlockToCFArrayCopiedToStack + 314
16 UIKit 0x00000001104f21ab _afterCACommitHandler + 516
17 CoreFoundation 0x000000010fdd4dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18 CoreFoundation 0x000000010fdd4d20 __CFRunLoopDoObservers + 368
19 CoreFoundation 0x000000010fdcab53 __CFRunLoopRun + 1123
20 CoreFoundation 0x000000010fdca486 CFRunLoopRunSpecific + 470
21 GraphicsServices 0x00000001143a09f0 GSEventRunModal + 161
22 UIKit 0x00000001104cf420 UIApplicationMain + 1282
23 MobileChallenge 0x000000010f5e5f13 main + 115
24 libdyld.dylib 0x0000000112672145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
主线程被阻塞。下面的代码成功了。
dispatch_async(kBgQueue, ^{
NSData *calenderData = [NSData dataWithContentsOfURL:kTournamentCaldenerURL];
[self performSelectorOnMainThread:@selector(fetchedCalender:) withObject:calenderData
waitUntilDone:YES];
});
将 nil 传递给 JSONObjectWithData: 是一个编程错误,会导致抛出异常,从而导致应用程序终止。此问题已通过修复编程错误得到解决。
这就是 Objective-C 中几乎所有异常的工作原理:它们在那里指示编程错误,您可以通过修复代码中的错误来解决问题。
我使用 kimono 创建了自己的 api,我正尝试使用此 api 来解析 json 数据。这是我 运行 应用程序时发生的错误。我很确定 url 地址或 api 键没有拼写错误,因为我从 kimono 复制并粘贴了它们。有什么建议吗?
提前致谢。
#import "PhotoGalleryTableViewController.h"
#define API_KEY @"xgp4nU6xA9UcBWSe0MIHcBVbAWz5v4wR"
@interface PhotoGalleryTableViewController ()
{
NSMutableArray *myObject;
NSDictionary *dictionary;
NSString *description;
NSString *photo;
}
@end
@implementation PhotoGalleryTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
photo = @"photo";
description = @"description";
myObject = [[NSMutableArray alloc] init];
NSString *urlAsString = [ NSString stringWithFormat:@"https://www.kimonolabs.com/api/2v35aqn0?apikey=xgp4nU6xA9UcBWSe0MIHcBVbAWz5v4wR"];
NSData *jsonSource = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlAsString]];
NSLog(@"%@", jsonSource);
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource
options:NSJSONReadingMutableContainers
error:nil];
for(NSDictionary *dataDict in jsonObjects) {
NSString *photo_data = [dataDict objectForKey:@"photo"];
NSString *description_data = [dataDict objectForKey:@"description"];
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
photo_data, photo,
description_data, description,
nil];
[myObject addObject:dictionary];
}
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
-( NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return myObject.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier= @"item";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
NSMutableString *text= [NSString stringWithFormat:@"%@",
[tmpDict objectForKeyedSubscript:description]];
NSMutableString *images;
images = [NSMutableString stringWithFormat:@"%@",
[tmpDict objectForKey:photo]];
NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:photo]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.textLabel.text = text;
cell.imageView.frame = CGRectMake(0, 0, 80, 70);
cell.imageView.image = img;
// Configure the cell...
return cell;
}
@end
堆栈跟踪:
stacktrace : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fdb6b65a890'
*** First throw call stack:
(
0 CoreFoundation 0x000000010fe9ff35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010fb38bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010fea704d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010fdff27c ___forwarding___ + 988
4 CoreFoundation 0x000000010fdfee18 _CF_forwarding_prep_0 + 120
5 MobileChallenge 0x000000010f5e40d1 -[PhotoGalleryTableViewController viewDidLoad] + 817
6 UIKit 0x00000001105f4a90 -[UIViewController loadViewIfRequired] + 738
7 UIKit 0x00000001105f4c8e -[UIViewController view] + 27
8 UIKit 0x0000000110b9641e -[_UIFullscreenPresentationController _setPresentedViewController:] + 65
9 UIKit 0x00000001105d0429 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 105
10 UIKit 0x0000000110600a41 -[UIViewController _presentViewController:withAnimationController:completion:] + 1746
11 UIKit 0x0000000110602d81 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 132
12 UIKit 0x0000000110602ca5 -[UIViewController presentViewController:animated:completion:] + 229
13 UIKit 0x00000001105b7360 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1242
14 UIKit 0x00000001105b74d4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
15 UIKit 0x00000001104f2331 _applyBlockToCFArrayCopiedToStack + 314
16 UIKit 0x00000001104f21ab _afterCACommitHandler + 516
17 CoreFoundation 0x000000010fdd4dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18 CoreFoundation 0x000000010fdd4d20 __CFRunLoopDoObservers + 368
19 CoreFoundation 0x000000010fdcab53 __CFRunLoopRun + 1123
20 CoreFoundation 0x000000010fdca486 CFRunLoopRunSpecific + 470
21 GraphicsServices 0x00000001143a09f0 GSEventRunModal + 161
22 UIKit 0x00000001104cf420 UIApplicationMain + 1282
23 MobileChallenge 0x000000010f5e5f13 main + 115
24 libdyld.dylib 0x0000000112672145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
主线程被阻塞。下面的代码成功了。
dispatch_async(kBgQueue, ^{
NSData *calenderData = [NSData dataWithContentsOfURL:kTournamentCaldenerURL];
[self performSelectorOnMainThread:@selector(fetchedCalender:) withObject:calenderData
waitUntilDone:YES];
});
将 nil 传递给 JSONObjectWithData: 是一个编程错误,会导致抛出异常,从而导致应用程序终止。此问题已通过修复编程错误得到解决。
这就是 Objective-C 中几乎所有异常的工作原理:它们在那里指示编程错误,您可以通过修复代码中的错误来解决问题。