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 · Feb 26, 2018 · 6837 views
    This topic created in 2999 days ago, the information mentioned may be changed or developed.

    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 replies    2018-06-16 16:29:21 +08:00
    timelessg
        1
    timelessg  
       Feb 26, 2018 via iPhone
    比较蠢的方法,子类方法拿到 super 方法的 return,加上子类的 key value 再 return,楼下继续
    holonunu
        2
    holonunu  
    OP
       Feb 27, 2018
    @timelessg 这方法下不了手:) 再看看有没有别的方法,没有就只能这样了
    ai277014717
        3
    ai277014717  
       Feb 27, 2018
    把 1 楼的方法封装一下,换个名字,以后都用新方法。
    Alchemistxxd
        4
    Alchemistxxd  
       Feb 27, 2018
    call super。还有就是最好少用入侵性这么强的底层 Model。
    holonunu
        5
    holonunu  
    OP
       Feb 27, 2018
    @Alchemistxxd 是协议,不算入侵吧。只是继承就意味着覆盖。
    Alchemistxxd
        6
    Alchemistxxd  
       Feb 27, 2018
    @holonunu 嗯看错了,太久没用 OC,联想成 Mantle 了
    gunavy
        7
    gunavy  
       Mar 22, 2018
    modle 多了,还不如直接用字典
    getinlight
        8
    getinlight  
       Jun 16, 2018
    变成一个属性 可好
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5814 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 155ms · UTC 01:55 · PVG 09:55 · LAX 18:55 · JFK 21:55
    ♥ Do have faith in what you're doing.