0
votes
{
    "restaurants" : [
    {
        "name": "Hopdoddy Burger Bar",
        "backgroundImageURL": "http://sandbox.bottlerocketapps.com/BR_iOS_CodingExam_2015_Server/Images/hopdoddy.png",
        "category" : "Burgers",
            "contact": {
                "phone": "9723872337",
                "formattedPhone": "(972) 387-2337",
                "twitter": "hopdoddy"
            },
            "location": {
                "address": "5100 Belt Line Road, STE 502",
                "crossStreet": "Dallas North Tollway",
                "lat": 32.950787,
                "lng": -96.821118,
                "postalCode": "75254",
                "cc": "US",
                "city": "Addison",
                "state": "TX",
                "country": "United States",
                "formattedAddress": [
                    "5100 Belt Line Road, STE 502 (Dallas North Tollway)",
                    "Addison, TX 75254",
                    "United States"
                ]
            }
    },
    {
        "name": "Pappadeaux Seafood Kitchen",
        "backgroundImageURL": "http://sandbox.bottlerocketapps.com/BR_iOS_CodingExam_2015_Server/Images/pappadeaux.png",
        "category": "Seafood",
            "contact": {
                "phone": "9724479616",
                "formattedPhone": "(972) 447-9616",
                "twitter": "pappadeaux"
            },
            "location": {
                "address": "18349 Dallas Pkwy",
                "crossStreet": "at Frankford Rd.",
                "lat": 32.99908456526653,
                "lng": -96.83018780592823,
                "postalCode": "75287",
                "cc": "US",
                "city": "Dallas",
                "state": "TX",
                "country": "United States",
                "formattedAddress": [
                    "18349 Dallas Pkwy (at Frankford Rd.)",
                    "Dallas, TX 75287",
                    "United States"
                ]
            }
    },
{
        "name": "Buffalo Wild Wings",
        "backgroundImageURL": "http://sandbox.bottlerocketapps.com/BR_iOS_CodingExam_2015_Server/Images/buffalo_wild_wings.png",
        "category": "Wing Joint",
            "contact": {
                "phone": "9727019464",
                "formattedPhone": "(972) 701-9464",
                "twitter": "bwwings"
            },
            "location": {
                "address": "5000 Belt Line Rd Ste 100",
                "crossStreet": "at Quorum Dr",
                "lat": 32.95347617827522,
                "lng": -96.82554602622986,
                "postalCode": "75254-6752",
                "cc": "US",
                "city": "Dallas",
                "state": "TX",
                "country": "United States",
                "formattedAddress": [
                    "5000 Belt Line Rd Ste 100 (at Quorum Dr)",
                    "Dallas, TX 75254-6752",
                    "United States"
                ]
            }
    }
    ]
}

This is the JSON I am trying to parse in iOS. I have created my Model objects with all the required parameters. I am able to parse and fetch all the data except for the backgroundImageURL. I get a NSUnknownKeyException. Can someone help me with this?

Below is the code used for creating the Model objects and adding them to an array.

 [self.restaurantsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        Restaurant *restaurant = [[Restaurant alloc]init];
        restaurant.name = [obj objectForKey:@"name"];
        restaurant.imageURL = [obj objectForKey:@"backgroundImageURL"];
        restaurant.category = [obj objectForKey:@"category"];
        restaurant.contact = [obj objectForKey:@"contact"];
        restaurant.location = [obj objectForKey:@"location"];
        [self.parsedRestaurantArray addObject:restaurant];

    }];  

This is the Restaurant class:

@interface Restaurant : NSObject

@property (nonatomic, strong)NSString *name;
@property (nonatomic, strong)NSString *imageURL;
@property (nonatomic, strong)NSString *category;
@property (nonatomic, strong)Contact *contact;
@property (nonatomic, strong)Location *location;

@end

Here is the track trace:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key backgroundImageURL.' *** First throw call stack: ( 0 CoreFoundation 0x000000010e631e65 exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010e0aadeb objc_exception_throw + 48 2 CoreFoundation 0x000000010e631aa9 -[NSException raise] + 9 3 Foundation 0x000000010dd0a888 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226 4 Foundation 0x000000010dc60997 -[NSObject(NSKeyValueCoding) valueForKey:] + 280 5 Foundation 0x000000010dc60758 -[NSArray(NSKeyValueCoding) valueForKey:] + 437 6 BottleRocketExercise 0x000000010cd8692a -[LunchesViewController collectionView:cellForItemAtIndexPath:] + 282 7 UIKit 0x000000010f2335ba -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] + 483 8 UIKit 0x000000010f235ae0 -[UICollectionView _updateVisibleCellsNow:] + 4431 9 UIKit 0x000000010f23a23b -[UICollectionView layoutSubviews] + 247 10 UIKit 0x000000010ea954a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 11 QuartzCore 0x00000001129a759a -[CALayer layoutSublayers] + 146 12 QuartzCore 0x000000011299be70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 13 QuartzCore 0x000000011299bcee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 14 QuartzCore 0x0000000112990475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 15 QuartzCore 0x00000001129bdc0a _ZN2CA11Transaction6commitEv + 486 16 QuartzCore 0x00000001129be37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 17 CoreFoundation 0x000000010e55d367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 18 CoreFoundation 0x000000010e55d2d7 __CFRunLoopDoObservers + 391 19 CoreFoundation 0x000000010e552f2b __CFRunLoopRun + 1147 20 CoreFoundation 0x000000010e552828 CFRunLoopRunSpecific + 488 21 GraphicsServices 0x000000011245cad2 GSEventRunModal + 161 22 UIKit 0x000000010e9de610 UIApplicationMain + 171 23 BottleRocketExercise 0x000000010cd85f5f main + 111 24 libdyld.dylib 0x00000001106f192d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

1
show your full code and whole jsonhariszaman
Please show full stack trace from this exception (NSUnknownKeyException)shpasta
we need more information. Show Your Restaurant Class and the full jsonJonas
I have included more information.user1342592
I think i know what went wrong. can you show how you load the json?Jonas

1 Answers

1
votes

I think the problem with your code is that you have and array of restaurants and not just one restaurant. Your obj has not the keys because it has arrays of restaurants. You can only get the keys of each restaurant.