V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
holonunu
V2EX  ›  iDev

使用 YYModel 时, Model 存在继承关系,如何处理?

  •  
  •   holonunu · 2018-02-26 19:42:56 +08:00 · 6091 次点击
    这是一个创建于 2248 天前的主题,其中的信息可能已经有所发展或是发生改变。

    YYModel 中可以使用以下两个方法处理 Model-JSON 转换的特殊情况:

    //①定义字段-属性的映射,如字段为"userID"转换到 userId 属性
    + (NSDictionary *)modelCustomPropertyMapper;
    
    //②验证 /处理字段的值,如字段值为"T"则赋值为 YES
    - (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic;
    

    如果 Model 之间存在继承关系:

    User
    - int userId(api 返回 userID,需要实现①)
    - 枚举 gender(api 返回 T/F 字符串,需要实现②转换成枚举)
    
    LoginUser : User
    - int loginId()(api 返回 loginID,需要实现①)
    - bool isVip(api 返回 T/F 字符串,需要实现②转换成布尔)
    

    问题是: LoginUser 实现的①、②会覆盖 User 实现的①、②,大家通常是怎么处理的呢?

    8 条回复    2018-06-16 16:29:21 +08:00
    timelessg
        1
    timelessg  
       2018-02-26 20:05:03 +08:00 via iPhone
    比较蠢的方法,子类方法拿到 super 方法的 return,加上子类的 key value 再 return,楼下继续
    holonunu
        2
    holonunu  
    OP
       2018-02-27 09:50:47 +08:00
    @timelessg 这方法下不了手:) 再看看有没有别的方法,没有就只能这样了
    ai277014717
        3
    ai277014717  
       2018-02-27 12:58:14 +08:00
    把 1 楼的方法封装一下,换个名字,以后都用新方法。
    Alchemistxxd
        4
    Alchemistxxd  
       2018-02-27 16:54:09 +08:00
    call super。还有就是最好少用入侵性这么强的底层 Model。
    holonunu
        5
    holonunu  
    OP
       2018-02-27 18:01:18 +08:00
    @Alchemistxxd 是协议,不算入侵吧。只是继承就意味着覆盖。
    Alchemistxxd
        6
    Alchemistxxd  
       2018-02-27 18:12:15 +08:00
    @holonunu 嗯看错了,太久没用 OC,联想成 Mantle 了
    gunavy
        7
    gunavy  
       2018-03-22 15:19:19 +08:00
    modle 多了,还不如直接用字典
    getinlight
        8
    getinlight  
       2018-06-16 16:29:21 +08:00
    变成一个属性 可好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3337 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:39 · PVG 19:39 · LAX 04:39 · JFK 07:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.