Objectmapper: Trying to map, but class does not conform the protocol 'Mappable'

Created on 9 Sep 2015  路  5Comments  路  Source: tristanhimmelman/ObjectMapper

I would like to map a JSON that I am receiving, but I am getting a message saying that:
Type 'City' does not conform to protocol 'Mappable'.

This is my class definition:

public protocol Mappable {
static func newInstance(map: Map) -> Mappable?
mutating func mapping(map: Map)
}

class City: Mappable {
var state: String?
var name: String?
var id: String?

class func newInstance(map: Map) -> Mappable? {
    return City()
}

func mapping(map: Map) {
    state   <- map["state"]
    name    <- map["name"]
    id      <- map["id"]
}

}

and I trying to map using this line:

let city = Mapper< City >().map(data)

However, I am getting the error message I have mentioned.

Most helpful comment

class City: Mappable {
    var state: String?
    var name: String?
    var id: String?

    class func newInstance(map: Map) -> Mappable? {
        return City()
    }

    func mapping(map: Map) {
        state   <- map["state"]
        name    <- map["name"]
        id      <- map["id"]
    }
}

The above class definition works when I test it. Mapping is done with the following code:

let city = Mapper<City>().map(JSON)

Are you including the Mappable protocol code within your class? If so, you should not. All you need to do is import ObjectMapper at the top of your class

import ObjectMapper

All 5 comments

Sorry it seems that < City > is considered as a comment by GitHub when you do not write spaces after or before the opening closing operator, so although it was written, it didn't appear.

class City: Mappable {
    var state: String?
    var name: String?
    var id: String?

    class func newInstance(map: Map) -> Mappable? {
        return City()
    }

    func mapping(map: Map) {
        state   <- map["state"]
        name    <- map["name"]
        id      <- map["id"]
    }
}

The above class definition works when I test it. Mapping is done with the following code:

let city = Mapper<City>().map(JSON)

Are you including the Mappable protocol code within your class? If so, you should not. All you need to do is import ObjectMapper at the top of your class

import ObjectMapper

Thanks it worked. I was including the Mappable protocol at the beginning of the file. Once again thank you so much.

@tristanhimmelman I still see this problem. Following is my code:

import Foundation
import ObjectMapper

class Thumb: Mappable {
    var thumbnail_480_url : String?
    var thumbnail_720_url : String?
    var thumbnail_url : String?

    class func newInstance(map: Map) -> Mappable? {
        return Thumb()
    }

    func mapping(map: Map) {
        thumbnail_url <- map["thumbnail_url"]
        thumbnail_480_url <- map["thumbnail_480_url"]
        thumbnail_720_url <- map["thumbnail_720_url"]
    }
}

I have a class something like below:

class City: Mappable {
    var state: String?
    var name: String?
    var id: String?

    class func newInstance(map: Map) -> Mappable? {
        return City()
    }

    func mapping(map: Map) {
        state   <- map["state"]
        name    <- map["name"]
        id      <- map["id"]
    }
}

and a variable:
var cityList: [City]?
Now I want to map jsonString to cityList. How should I do this?

Was this page helpful?
0 / 5 - 0 ratings