tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.15k stars 1.03k forks source link

In argument type 'String.Type', 'String' does not conform to expected type 'Mappable' #990

Open sheng930920 opened 6 years ago

sheng930920 commented 6 years ago

let's say I have a json object that might look like this:

{
  "code": 0,
  "result": "https://github.com/Hearst-DD/ObjectMapper",
  "msg": "Unknown error, please check with Admin."
}

My model:

class HttpResponse<T : Mappable> : Mappable {

    var code: Int = 0
    var msg: String?
    var result: T?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        code    <- map["code"]
        msg    <- map["msg"]
        result    <- map["result"]
    }

}

class User: Mappable {
    var username: String?
    var age: Int?
    var weight: Double!
    var array: [Any]?
    var dictionary: [String : Any] = [:]
    var bestFriend: User?                       // Nested User object
    var friends: [User]?                        // Array of Users
    var birthday: Date?

    required init?(map: Map) {

    }

    // Mappable
    func mapping(map: Map) {
        username    <- map["username"]
        age         <- map["age"]
        weight      <- map["weight"]
        array       <- map["arr"]
        dictionary  <- map["dict"]
        bestFriend  <- map["best_friend"]
        friends     <- map["friends"]
        birthday    <- (map["birthday"], DateTransform())
    }
}

But the value of result is generic, the value type has String, or the entity object like User, when I pass in the String error, the prompt 'String' does not conform to expected type 'Mappable'

I did:


let result1 = Mapper<HttpResponse<User>>().map(JSON)

let result2 = Mapper<HttpResponse<String>>().map(JSON)

result2 tost the prompt 'String' does not conform to expected type 'Mappable'

I exepected something like:

let result2 = Mapper<HttpResponse<String>>().map(JSON)
nawinkhatiwada commented 5 years ago

I am having the same issue.

SoufianHossam commented 5 years ago

any luck with this issue ?