“[__NSDictionaryI content]: unrecognized selector sent to instance”从自定义 JsonModel 获取数据时
Posted
技术标签:
【中文标题】“[__NSDictionaryI content]: unrecognized selector sent to instance”从自定义 JsonModel 获取数据时【英文标题】:"[__NSDictionaryI content]: unrecognized selector sent to instance" When getting data from Custom JsonModel 【发布时间】:2017-10-27 02:55:51 【问题描述】:我已经实现了以下 JsonModels。
@interface MainModel : JSONModel
@property (nonatomic) NSArray<CCLSecurityChecklistModel *> *cclSecurityChecklist;
@end
@interface CCLSecurityChecklistModel : JSONModel
@property (nonatomic) NSString *section;
@property (nonatomic) NSString *location;
@property (nonatomic) NSString *nfc;
@property (nonatomic) NSArray <ContentModel *> *content;
@end
@interface ContentModel : JSONModel
@property (nonatomic) NSString *question;
@property (nonatomic) NSString *type;
@property (nonatomic) NSString *required;
@property (nonatomic) NSString *extra;
@property (nonatomic) NSArray *choices;
@property (strong, nonatomic) AnswerModel *answer;
@end
这是我的 Json。
"cclSecurityChecklist" :[
"section": "Security Checklist - CTSG (1)",
"location": "",
"nfc": true,
"content": [
"question": "",
"type": "checkbox",
"required": true,
"extra": true,
"choices": ["Exits/ Entrances", "Air in-take shafts", "Bicycle Parking Area", "Dustbin area near entrance", "Subway Area",
"Concourse", "Lifts", "Non-lockable compartments along passage way (both paid & unpaid areas)", "Fire equipment cabinets",
"Emergency Exits", "Doors leading to controlled area/non-public area are locked", "Staircase landings", "Platforms",
"End of platforms", "Seating area"],
"answer":
"value": [],
"photos": [],
"remarks": []
]
,
"section": "Security Checklist - CTSG (2)",
"location": "Concourse - public area",
"nfc": true,
"content": [
"question": "",
"type": "checkbox",
"required": true,
"extra": true,
"choices": ["AED - a)Housing Cabinet in good condition. b)Signage is intact and displayed. c) glass piece for key pocket is not broken.d) key is present. e) Green light indication on the AED is blinking.e) Green light indication on the AED is blinking.e) Green light indication on the AED is blinking.e) Green light indication on the AED is blinking"],
"answer":
"value": [],
"photos": [],
"remarks": []
]
]
这是我在 AppDelegate 中将 JsonString 转换为 MainModel 的方法。
- (NSArray<CCLSecurityChecklistModel *> *)convertJsonToModel:(NSString *)fileName
NSError *error;
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSString * myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
MainModel *model= [[MainModel alloc] initWithString:myString error:&error];
return model.cclSecurityChecklist;
这是我打印出 MainModel 时的结果。
<MainModel>
[cclSecurityChecklist]: (
content = (
answer =
photos = (
);
remarks = (
);
value = (
);
;
choices = (
"Exits/ Entrances",
"Air in-take shafts",
"Bicycle Parking Area",
"Dustbin area near entrance",
"Subway Area",
Concourse,
Lifts,
"Non-lockable compartments along passage way (both paid & unpaid areas)",
"Fire equipment cabinets",
"Emergency Exits",
"Doors leading to controlled area/non-public area are locked",
"Staircase landings",
Platforms,
"End of platforms",
"Seating area"
);
extra = 1;
question = "";
required = 1;
type = checkbox;
);
location = "";
nfc = 1;
section = "Security Checklist - CTSG (1)";
,
content = (
answer =
photos = (
);
remarks = (
);
value = (
);
;
choices = (
"AED - a)Housing Cabinet in good condition. b)Signage is intact and displayed. c) glass piece for key pocket is not broken.d) key is present. e) Green light indication on the AED is blinking.e) Green light indication on the AED is blinking.e) Green light indication on the AED is blinking.e) Green light indication on the AED is blinking"
);
extra = 1;
question = "";
required = 1;
type = checkbox;
);
location = "Concourse - public area";
nfc = 1;
section = "Security Checklist - CTSG (2)";
)
</MainModel>
这里是从其他 ViewController 中的 JsonModel 的 Object 中检索数据。
但是当我从 CCLSecurityChecklistModel 的对象中检索内容时出现此错误。
-[__NSDictionaryI content]: unrecognized selector sent to instance 0x6000000f3280
这是我第一次使用 JsonModel Mapper。任何人都可以建议我并帮助我解决这个问题。我被困在这里。抱歉解释太长了。
【问题讨论】:
错误信息显示self.cclSecurityChecklist
包含NSDictionary
而不是预期的CCLSecurityChecklistModel
对象
self.contentModels
是什么?
@SushilSharma NSArray幸运的是,我根据上述答案的提示找到了解决方案。我把Json格式改成了这个
[
"section": "Security Checklist - CTSG (1)",
"location": "",
"nfc": true,
"content": [
"question": "",
"type": "checkbox",
"required": true,
"extra": true,
"choices": ["Exits/ Entrances", "Air in-take shafts", "Bicycle Parking Area", "Dustbin area near entrance", "Subway Area",
"Concourse", "Lifts", "Non-lockable compartments along passage way (both paid & unpaid areas)", "Fire equipment cabinets",
"Emergency Exits", "Doors leading to controlled area/non-public area are locked", "Staircase landings", "Platforms",
"End of platforms", "Seating area"],
"answer":
"value": [],
"photos": [],
"remarks": []
]
,
"section": "Security Checklist - CTSG (2)",
"location": "Concourse - public area",
"nfc": true,
"content": [
"question": "",
"type": "checkbox",
"required": true,
"extra": true,
"choices": ["AED - a)Housing Cabinet in good condition. b)Signage is intact and displayed. c) glass piece for key pocket is not broken.d) key is present. e) Green light indication on the AED is blinking.e) Green light indication on the AED is blinking.e) Green light indication on the AED is blinking.e) Green light indication on the AED is blinking"],
"answer":
"value": [],
"photos": [],
"remarks": []
]
]
并把Converting Json to Model的代码改成这个。
- (NSArray<CCLSecurityChecklistModel *> *)convertJsonToModel:(NSString *)fileName
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSString * myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableArray *yourArray = [CCLSecurityChecklistModel arrayOfModelsFromString:myString error:nil];
MainModel *objCardData = [[MainModel alloc] init];
objCardData.cclSecurityChecklist = yourArray;
return objCardData.cclSecurityChecklist;
根据这个答案。 parse json using JsonModel
非常感谢您的宝贵时间。
【讨论】:
以上是关于“[__NSDictionaryI content]: unrecognized selector sent to instance”从自定义 JsonModel 获取数据时的主要内容,如果未能解决你的问题,请参考以下文章