使用以下模型作爲示例,在JSONModel中處理多態性的最佳實踐是什麼?JSONModel iOS和多態性
@interface GameModel : JSONModel
@property (nonatomic, assign) long id;
@property (nonatomic, assign) NSArray<GameEventModel> *events;
/*
...
*/
@end
@interface GameEventModel : JSONModel
@property (nonatomic, assign) long long timestamp;
/*
...
*/
@end
@interface GameTouchEventModel : GameEventModel
@property (nonatomic, assign) CGPoint point;
/*
...
*/
@end
當GameModel用的{id:1, events:[{point:{x:1, y:1}, timestamp:...}]}
JSON字符串開始JSONModel將使用GameEventModel
而忽略了point
財產。
它會更好用一個通用的GameEventModel
含有type
財產和財產info
...等
@interface GameTouchEventModel : GameEventModel
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSDictionary *info;
@end
,因此模型可以接受JSON作爲{id:1, events:[{ type:"GameTouchEventModel", info:{ point:{x:1, y:1}, timestamp:... } }]}
與此問題方法很難讀取代碼,並且沒有編譯器警告/錯誤等。
有沒有辦法在JSONModel中使用多態模型?