请稍侯

jsonmapple 与 mje

22 May 2023

JSONMapple 与 MJExtension 处理

@objcMembers
public class XXItemModel: NSObject, JSONMappable {
    public var userName: String?
    public var userId: Int64?
    public var xxType: Any?

    var itemType: XXEnumType? {
        switch xxType {
        case let t as XXEnumType:
            return t
        case let t as Int:
            return XXEnumType(intValue: t)
        case let t as String:
            return XXEnumType(rawValue: t)
        default:
            return .none
        }
    }

    //ignore mjextension en/decode property
    public override class func mj_ignoredPropertyNames() -> [Any] {
        ["itemType"]
    }

    public static func mappedObject(from jsonDictionary: [String: Any], _: RTError?) -> JSONMappable? {
        let model = XXItemModel()
        model.userId = jsonDictionary.number(forKey: "userId")?.int64Value
        model.userName = jsonDictionary.string(forKey: "userName")
        if let intValue = jsonDictionary.int(forKey: "xxType"),
           let type = XXEnumType(intValue: intValue) {
            model.xxType = type
        }else if let stringValue = jsonDictionary.string(forKey: "xxType"),
                 let type = XXEnumType(rawValue: stringValue) {
            model.xxType = type
        }
        return model
    }

}

MJExtension 中属性转换

//- (void)mj_didConvertToObjectWithKeyValues:(NSDictionary *)keyValues {
//    if(_a != nil && [_a isKindOfClass: NSDictionary.class]) {
//        _a = [XXItemModel mj_objectWithKeyValues:_b];
//    }
//    if(_b != nil && [_chatReply isKindOfClass: NSDictionary.class]) {
//        _b = [YYItemModel mj_objectWithKeyValues:_b];
//    }
//}

+ (NSDictionary *)mj_objectClassInArray {
    return @{
        @"_aMsgList": AMessage.class
    };
}

//- (void)mj_keyValuesDidFinishConvertingToObject {
//    if(_aMsgList != nil) {
//        _aMsgList = [AMessage mj_objectArrayWithKeyValuesArray: _aMsgList];
//    }
//}