JSON:由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:'数据参数为nil'

时间:2015-01-06 08:35:13

标签: objective-c json ios7 xcode6

我使用和服创建了自己的api,我试图使用这个api来解析json数据。这是我运行应用程序时发生的错误。我非常确定url地址或api密钥没有拼写错误,因为我从和服复制并粘贴了它们。有什么建议吗?

提前谢谢。

#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) 

2 个答案:

答案 0 :(得分:0)

主线程已被阻止。下面的代码完成了诀窍。

dispatch_async(kBgQueue, ^{
    NSData *calenderData = [NSData dataWithContentsOfURL:kTournamentCaldenerURL];
    [self performSelectorOnMainThread:@selector(fetchedCalender:) withObject:calenderData
                        waitUntilDone:YES];
});

答案 1 :(得分:0)

将nil传递给JSONObjectWithData:是一个编程错误,导致抛出异常,导致应用程序终止。通过修复编程错误可以解决此问题。

这就是Objective-C中几乎所有异常的工作原理:它们用于指示编程错误,您可以通过修复代码中的错误来解决问题。

相关问题