gpt4 book ai didi

ios - JSONModel iOS 和多态性

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:12 31 4
gpt4 key购买 nike

使用以下模型作为示例,在 JSONModel 中处理多态性的最佳实践是什么? ?

@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 字符串启动时p>

JSONModel 将使用 GameEventModel 并忽略 point 属性。

使用包含type 属性和info 属性的通用GameEventModel 是否更好,例如...

@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中没有办法使用多态模型吗?

最佳答案

我们通过对 JSONModel.m 进行 2 次小改动解决了这个问题,引入了一个新的特殊 JSON 属性 __subclass,它由 JSONModel 获取解析器并使用该值作为对象类型。 __subclass 必须是保留关键字(因此任何模型都不能使用 __subclass 作为属性名称)。

JSONModel.m 的更改

// ...
-(id)initWithDictionary:(NSDictionary*)dict error:(NSError**)err
{
// ...
if ([self __isJSONModelSubClass:property.type]) {

//initialize the property's model, store it
JSONModelError* initErr = nil;

-- id value = [[property.type alloc] initWithDictionary: jsonValue error:&initErr];

++ id value;
++ if([jsonValue valueForKey:@"subclass"] != NULL)
++ {
++ Class jsonSubclass = NSClassFromString([d valueForKey:@"subclass"]);
++ if(jsonSubclass)
++ obj = [[jsonSubclass alloc] initWithDictionary:d error:&initErr];
++ }
++ else
++ value = [[property.type alloc] initWithDictionary: jsonValue error:&initErr];
//...
//...
+(NSMutableArray*)arrayOfModelsFromDictionaries:(NSArray*)array error:(NSError**)err
{
// ...
for (NSDictionary* d in array) {
JSONModelError* initErr = nil;

-- id obj = [[self alloc] initWithDictionary:d error:&initErr];

++ id obj;
++ if([d valueForKey:@"subclass"] != NULL)
++ {
++ Class jsonSubclass = NSClassFromString([d valueForKey:@"subclass"]);
++ if(jsonSubclass)
++ obj = [[jsonSubclass alloc] initWithDictionary:d error:&initErr];
++ }
++ else
++ obj = [[self alloc] initWithDictionary:d error:&initErr];
// ...
// ...

注意:如果 _subclass 的 JSON 模型类不存在,则该模型将回退到父类(super class)。

这将适用于以下模型

@interface GameModel : JSONModel
@property (nonatomic, assign) long id;
@property (nonatomic, assign) NSArray<GameEventModel> *events;
@end

@protocol GameEventModel
@end

@interface GameEventModel : JSONModel
@property (nonatomic, assign) long long timestamp;
@end

@interface GameTouchEventModel : GameEventModel
@property (nonatomic, strong) NSArray *point;
@end

当传递JSON字符串时{id:1, events:[ { __subclass:'GameTouchEventModel', timestamp:1, point: [0,0] } ] }

关于ios - JSONModel iOS 和多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22159573/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com