无法在iOS

时间:2016-02-15 21:39:19

标签: ios objective-c

{
    "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"
                ]
            }
    }
    ]
}

这是我试图在iOS中解析的JSON。我用所有必需的参数创建了我的Model对象。我能够解析并获取除backgroundImageURL之外的所有数据。我得到一个NSUnknownKeyException。有人可以帮我这个吗?

下面是用于创建Model对象并将它们添加到数组中的代码。

 [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];

    }];  

这是餐厅课程:

@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

以下是曲目追踪:

由于未捕获的异常终止应用程序' NSUnknownKeyException',原因:' [valueForUndefinedKey:]:此类不是密钥backgroundImageURL的密钥值编码兼容。' ***第一次抛出调用堆栈: (     0 CoreFoundation 0x000000010e631e65 exceptionPreprocess + 165     1 libobjc.A.dylib 0x000000010e0aadeb objc_exception_throw + 48     2 CoreFoundation 0x000000010e631aa9 - [NSException raise] + 9     3基础0x000000010dd0a888 - [NSObject(NSKeyValueCoding)valueForUndefinedKey:] + 226     4基础0x000000010dc60997 - [NSObject(NSKeyValueCoding)valueForKey:] + 280     5基金会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:以NSException类型的未捕获异常终止

1 个答案:

答案 0 :(得分:1)

我认为您的代码存在的问题是您拥有一系列餐馆而不仅仅是一家餐馆。你的obj没有钥匙,因为它有餐厅阵列。您只能获得每家餐厅的钥匙。