ios - Dynamic model cascading based on property with JSONModel -
given following json blob:
[ { type: "audio", title: "audio example title", }, { type: "video", title: "video example title", }, { type: "audio", title: "another audio example title", }, ]
and 2 jsonmodel model classes (audiomodel, videomodel). possible have jsonmodel automatically create either 1 of model classes based on type
property when maps json models?
it's possible using for..in
loop , checking type property , creating model object based on type below
nsmutablearray *audiomodelarray = [nsmutablearray alloc] init]; nsmutablearray *videomodelarray = [nsmutablearray alloc] init]; for(nsdictionary *jsondict in jsonarray) { if(jsondict[@"type"] isequaltostring:@"audio") { audiomodel *audio = [audiomodel alloc]initwithtitle:jsondict[@"title"]]; [audiomodelarray addobject: audio]; } else { videomodel *audio = [videomodel alloc] initwithtitle:jsondict[@"title"]]; [videomodelarray addobject: audio]; } }
then can iteration on audiomodelarray
, videomodelarray
objects access audomodel , videomodel objects , properties.
Comments
Post a Comment