@interface XYMessage : MTLModel @end @interface XYTextMessage: XYMessage @property (readonly, nonatomic, copy) NSString *body; @end @interface XYPictureMessage : XYMessage @property (readonly, nonatomic, strong) NSURL *imageURL; @end @implementation XYMessage + (Class)classForParsingJSONDictionary:(NSDictionary *)JSONDictionary { if (JSONDictionary[@"image_url"] != nil) { return XYPictureMessage.class; } if (JSONDictionary[@"body"] != nil) { return XYTextMessage.class; } NSAssert(NO, @"No matching class for the JSON dictionary '%@'.", JSONDictionary); return self; } @end
一个类簇的实现示例(来自mantle的github主页)
最新推荐文章于 2025-10-15 10:26:03 发布
本文介绍了一种用于iOS应用的消息模型设计,该模型支持不同类型的子类如文本消息和图片消息,并通过解析JSON字典来确定消息的具体类型。
2369

被折叠的 条评论
为什么被折叠?



