I am using AlamofireObjectMapper to parse json response to my object. The AlamofireObjectMapper is a extension of ObjectMapper.
According to their documents, my model class has to conform to Mappable
protocol. For example:
class Forecast: Mappable {
var day: String?
var temperature: Int?
var conditions: String?
required init?(_ map: Map){
}
func mapping(map: Map) {
day <- map["day"]
temperature <- map["temperature"]
conditions <- map["conditions"]
}
}
In order to conform to Mappable protocl, my model class has to implement the required initializer and the mapping function for each field. It makes sense.
BUT, how does it support struct
type? For example, I have a Coordinate
structure, I try to conform to the Mappable
protocol:
struct Coordinate: Mappable {
var xPos: Int
var yPos: Int
// ERROR: 'required' initializer in non-class type
required init?(_ map: Map) {}
func mapping(map: Map) {
xPos <- map["xPos"]
yPos <- map["yPos"]
}
}
I cannot make my Coordinate
conform to the Mappable, because of the error I show above.
(I think it is quite often that using struct
for coordinate data instead of using class
)
My questions:
Q1. Does AlamofireObjectMapper or ObjectMapper library support struct
type? How to use them parsing json response to a struct
type object then?
Q2. If those libraries doesn't support parsing json response to struct type object. What is the way to do so in iOS with Swift2 ?