I'm using Alamofire
, and Alamofire Object Mapper
.
Think this is the response from the web service:
{
"status": 200,
"error": false,
"response": {
"id": 9,
"parent_id": 0,
"company_id": 1,
"image": "",
"name": "Games",
"description": "Games",
"created_at": "2016-12-16 12:11:51",
"updated_at": "2016-12-16 12:11:51",
"deleted_at": null,
"Games": [
{
"id": 36,
"company_id": 1,
"application_id": 7,
"category_id": 9,
"start_date": "2016-12-16 00:00:00",
"end_date": "2016-12-27 00:00:00",
"status": "PUBLISH",
"created_at": "2016-12-16 13:29:16",
"updated_at": "2016-12-16 13:29:48",
"deleted_at": null
}
]
},
"error_messages": [],
"error_message": ""
}
from this response I want to get the Games
array.
here is my Games
model
var gameId : String!
var companyID : String!
var categoryID : String!
var startDate : String!
var endDate : String!
var status : String!
var buildertitle : String!
var builderdescription : String!
override init() {
super.init()
}
required convenience init?(map : Map) {
self.init()
}
func mapping(map: Map) {
gameId <- map["id"]
companyID <- map["company_id"]
categoryID <- map["category_id"]
startDate <- map["start_date"]
endDate <- map["end_date"]
status <- map["status"]
buildertitle <- map["title"]
builderdescription <- map["description"]
}
init(dic : NSDictionary) {
super.init()
gameId = Utils.nulltoEmpty(dic["id"] as AnyObject) as! String
companyID = Utils.nulltoEmpty(dic["company_id"] as AnyObject) as! String
categoryID = Utils.nulltoEmpty(dic["category_id"] as AnyObject) as! String
startDate = Utils.nulltoEmpty(dic["start_date"] as AnyObject) as! String
endDate = Utils.nulltoEmpty(dic["end_date"] as AnyObject) as! String
status = Utils.nulltoEmpty(dic["status"] as AnyObject) as! String
buildertitle = Utils.nulltoEmpty(dic["title"] as AnyObject) as! String
builderdescription = Utils.nulltoEmpty(dic["description"] as AnyObject) as! String
}
and here is my response
model
var status : Int!
var user : User?
var errorMessage : String?
var device : Device?
var games : Games?
required convenience init?(map : Map) {
self.init()
}
func mapping(map: Map) {
status <- map["status"]
user <- map["response"]
errorMessage <- map["error_message"]
device <- map["response"]
games <- map["Games"]
}
and finally this is my requst with alamofire
func getGamesList () {
let UrlReqeust = Router.getUserNews().urlRequest
Alamofire.request(UrlReqeust!).responseArray{ (response : DataResponse<[Games]>) in
print("The status code is : \(response.response?.statusCode)")
print("the response is : \(response)")
switch response.result {
case .success(let gamesbuilder):
print("the result is : \(gamesbuilder)")
break
case .failure(let Error):
break
}
}
}
- if I user
responseString
instead ofresponseArray
it gives aString
. else it gives an error with success status code like below.
"The status code is : Optional(200) the response is : FAILURE: Error Domain=com.alamofireobjectmapper.error Code=2 "ObjectMapper failed to serialize response." UserInfo={NSLocalizedFailureReason=ObjectMapper failed to serialize response.}"
The other part is, all the responses
comes under response
key. so how can I filter those.
hope your help with this.
Have you tried
responseArray(keyPath: "Games")
orresponseArray(keyPath: "response.Games")
? Because you mapping don't know how to parse all fields.