如何在地幔中串行化为原始类型?

时间:2016-01-08 10:43:58

标签: ios swift ios9.1 github-mantle

我正在使用带有iOS 9.1的Mantle进行申请

class SKUser: MTLModel, MTLJSONSerializing {

    var userId:NSNumber = -1
    var firstName:String = ""
    var lastName:String = ""

    var country:String! = ""
    var address:String! = ""
    var company:String! = ""
    var email:String! = ""
    var mobileNumber:NSNumber = -1

    static func JSONKeyPathsByPropertyKey() -> [NSObject : AnyObject]! {
        return [
            "userId": "user_id",
            "firstName": "user_first_name",
            "lastName": "user_last_name",
            "country": "user_country",
            "address": "user_address",
            "company": "user_company",
            "email": "user_email",
            "mobileNumber": "user_mobile_number"
        ]
    }
}

我无法将手机号码 userId 字段转换为整数或NSNumber,我已经尝试了很多但找不到解决方案

请帮帮我

提前致谢...

1 个答案:

答案 0 :(得分:0)

在代码中尝试此更改。 (斯威夫特3)

class SKUser: MTLModel, MTLJSONSerializing {  
    var userId:NSNumber?
    var firstName:String?
    var lastName:String?

    var country:String?
    var address:String?
    var company:String?
    var email:String?
    var mobileNumber:NSNumber?

   public override static func jsonKeyPathsByPropertyKey() -> [AnyHashable : Any]!  {
        return [
            "userId" as AnyHashable: "userId" as Any,
            "firstName" as AnyHashable: "firstName" as Any,
            "lastName" as AnyHashable: "lastName" as Any,
            "country" as AnyHashable: "country" as Any,
            "address" as AnyHashable: "address" as Any,
            "company" as AnyHashable: "company" as Any,
            "email" as AnyHashable: "email" as Any,
            "mobileNumber" as AnyHashable: "mobileNumber" as Any
        ]
    }
}

注意小写" json" in" jsonKeyPathsByPropertyKey"而是" JSONKeyPathsByPropertyKey"