Objectmapper: Binary operator '<-' cannot be applied to operands of type 'String' and '(Map, TransformOf<Int, String>)

Created on 8 Mar 2017  ·  3Comments  ·  Source: tristanhimmelman/ObjectMapper

Hi,
I am trying to map a json to an object, and there is a value (an integer) that is always nil.

This my json sample:

{"username":"user1","age":3}

And this is my code:

class T1: Mappable {
    var username: String?
    var age: String?

    required init?(map: Map) {

    }

    // Mappable
    func mapping(map: Map) {

        let transform = TransformOf<Int, String>(fromJSON: { (value: String?) -> Int? in
            // transform value from String? to Int?
            return Int(value!)
        }, toJSON: { (value: Int?) -> String? in
            // transform value from Int? to String?
            if let value = value {
                return String(value)
            }
            return nil
        })

        username    <- map["username"]
        age <- (map["age"], transform)

    }
}

Since the json is returning "age" without using "" I am trying to convert it to a String using the transformOF function, so I can store it in the "age" variable of type string (I cannot change the type of "age" to Int, since following I will be adding realm support and I need that variable to be a String).

Anyways, I cannot compile the project, due to this error when I try to apply the transform function:

Binary operator '<-' cannot be applied to operands of type 'String' and '(Map, TransformOf)'

How can I correctly use TransformOf to convert my integer to a string?

Thanks

Most helpful comment

@adasoft-dev, you're doing in the opposite way. TransformOf is defined as TransformOf<Object, JSON> which corresponds to String and Int.

let transform = TransformOf<String, Int>(
  fromJSON: { (value: Int?) -> String? in
    if let value = value {
      return String(value)
    } else {
      return nil
    }
  },
  toJSON: { (value: String?) -> Int? in
    if let value = value {
      return Int(value)
    } else {
      return nil
    }
  }
)

All 3 comments

+1

@adasoft-dev, you're doing in the opposite way. TransformOf is defined as TransformOf<Object, JSON> which corresponds to String and Int.

let transform = TransformOf<String, Int>(
  fromJSON: { (value: Int?) -> String? in
    if let value = value {
      return String(value)
    } else {
      return nil
    }
  },
  toJSON: { (value: String?) -> Int? in
    if let value = value {
      return Int(value)
    } else {
      return nil
    }
  }
)

I have the same problem. The server always return the value is int type or string type, sometime the value is changed, so I wish the defined type in the class, the value type in the json string could be transformed. SO I don't need to change the type in the class.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhengying picture zhengying  ·  4Comments

danyalaytekin picture danyalaytekin  ·  4Comments

pcompassion picture pcompassion  ·  3Comments

maksTheAwesome picture maksTheAwesome  ·  4Comments

VictorAlbertos picture VictorAlbertos  ·  3Comments