为什么我的JSONModel类被视为NSDictionary?

时间:2019-01-04 17:38:35

标签: objective-c api model jsonmodel

这是从API提取的JSON:

 {
categories =     (
            {
        icon =             {
            prefix =     "https://ss3.4sqi.net/img/categories_v2/food/italian_";
            suffix = ".png";
        };
        id = 4bf58dd8d48988d110941735;
        name = "Italian Restaurant";
        pluralName = "Italian Restaurants";
        primary = 1;
        shortName = Italian;
    }
);
hasPerk = 0;
id = 4b5bed2ef964a520971d29e3;
location =     {
    address = "HS-5";
    cc = IN;
    city = "New Delhi";
    country = India;
    crossStreet = "Kailash Colony Market";
    distance = 4061;
    formattedAddress =         (
        "HS-5 (Kailash Colony Market)",
        "New Delhi 110024",
        Delhi,
        India
    );
    labeledLatLngs =         (
                    {
            label = display;
            lat = "28.55272788981442";
            lng = "77.24192322690806";
        }
    );
    lat = "28.55272788981442";
    lng = "77.24192322690806";
    postalCode = 110024;
    state = Delhi;
};
name = "The Big Chill Cafe";
referralId = "v-1546605733";
} 

这是我的模型课:

@interface LocationModel : JSONModel

@property (nonatomic) NSInteger distance;
@property (nonatomic) NSInteger postalCode;
@property (nonatomic) NSString *address;
@property (nonatomic) NSString *cc;
@property (nonatomic) NSString *city;
@property (nonatomic) NSString *country;
@property (nonatomic) NSString *crossStreet;
@property (nonatomic) NSString *lat;
@property (nonatomic) NSString *lng;
@property (nonatomic) NSString *state;
@property (nonatomic) NSArray *formattedAddress;
@property (nonatomic) NSArray *labeledLatLngs;
@end


@protocol VenueModel;

@interface VenueModel : JSONModel

@property (nonatomic) NSArray *categories;
@property (nonatomic) BOOL hasPerk;
@property (nonatomic) NSString *id;
@property (nonatomic) LocationModel *location;
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *referralId;
@end

@interface Restaurant : JSONModel
@property (nonatomic) NSInteger confident;
@property (nonatomic) NSArray <VenueModel*> *venues;
@end

现在,我正在尝试获取如下值:

restaurant = [[Restaurant alloc] initWithDictionary

 [NSJSONSerialization JSONObjectWithData:JSON            
options:NSJSONReadingAllowFragments error:nil][@"response"] error:&error];

VenueModel *obj = [restaurant.venues firstObject];
LocationModel *locationObj = obj.location;

直到VenueModel对象的值都很好,之后,当我尝试访问LocationModel对象时,即

LocationModel *locationObj = obj.location

,它会引发以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI location]: unrecognized selector sent to instance 0x600001b82880

请有人帮帮我,我需要定位对象。

1 个答案:

答案 0 :(得分:2)

NSJSONSerialization将根据JSON字符串生成NSDictionaries,NSArrays,NSStrings,NSNumbers和其他一些基本类型。

您初始化自定义对象([[Restaurant alloc] initWithDictionary...)的想法是正确的想法,但是该想法也必须应用于嵌套结构。因此,例如,Restaurant初始化程序必须将该想法应用于其venues数组。

否则,您将遇到崩溃,将其中一个场所对象视为VenueModel而不是它的真实对象(NSDictionary)。

您没有发布initWithDictionary,但是它需要看起来像这样,尤其是其中包含了对象 的部分已初始化...

- (id)initWithDictionary:(NSDictionary *)dictionary {
    self = [super init];
    // ...
    self.venues = [NSMutableArray array];
    if (self) {
        for (NSDictionary *venueDictionary in dictionary[@"venues"]) {
            VenueModel *venue = [[VenueModel alloc] initWithDictionary:venueDictionary];
            [self.venues addObject:venue];
        }
    }
    // and so on, for every other object type nested in dictionary
    return self;
}

此外,如您所见,VenueModel还必须遵循此做法,将位置数据转换为LocationModel,依此类推...