-1

I created my own api by using kimono, and I'm trying to use this api to parse json data. This is the error that occured when I ran the app. I'm pretty sure that there is no spelling error with url address or api key, as I copied and pasted them from kimono. Any suggestions?

Thank you in advance.

#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

Stack trace:

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) 
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
Shawn Clarke
  • 35
  • 1
  • 1
  • 5

2 Answers2

0

Main thread was blocked. The code below did the trick.

dispatch_async(kBgQueue, ^{
    NSData *calenderData = [NSData dataWithContentsOfURL:kTournamentCaldenerURL];
    [self performSelectorOnMainThread:@selector(fetchedCalender:) withObject:calenderData
                        waitUntilDone:YES];
});
Shawn Clarke
  • 35
  • 1
  • 1
  • 5
0

Passing nil to JSONObjectWithData: is a programming error, which leads to an exception being thrown, which leads to termination of the app. This problem is fixed by fixing the programming error.

That's how almost all exceptions in Objective-C work: They are there to indicate programming errors, and you solve the issue by fixing the bug in your code.

gnasher729
  • 51,477
  • 5
  • 75
  • 98