You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you can see it's a list of rooms. Each room has a number and a name. I have created a model class called Room with two properties for ID and Name.
Here is the method I call the web service to get the response.
public func getRooms(completionHandler: (response: [Room]?, error: NSError?) -> Void) {
let response = {(response: NSHTTPURLResponse!, data: HTTPHandler.Data!, error: NSError!) -> Void in
let json = JSONValue(data)
println(json["rooms"])
}
let httphandler = HTTPHandler.get("http://webservie.com/rooms", response: response)
}
I want to return an array of Room class objects. I get the JSON response to the json constant properly. How can I iterate through it and collect the objects into an array? I tried to do a for in loop on it but apparently its not possible.
Can someone please help me out?
Thank you.
The text was updated successfully, but these errors were encountered:
let json = JSONValue(data)
switch json["rooms"] {
case .JObject(let rooms):
var roomsAray = [Room]()
for room in rooms {
var roomObj = Room(id: room.0.toInt(), type: room.1.string)
roomsAray.append(roomObj)
}
completionHandler(response: roomsAray, error: error)
default:
println("JSON data is broken!")
}
but your way is much cleaner and clearer. Thanks. 👍
I get a JSON response from a web service which looks like this.
As you can see it's a list of rooms. Each room has a number and a name. I have created a model class called Room with two properties for ID and Name.
Here is the method I call the web service to get the response.
I want to return an array of Room class objects. I get the JSON response to the json constant properly. How can I iterate through it and collect the objects into an array? I tried to do a for in loop on it but apparently its not possible.
Can someone please help me out?
Thank you.
The text was updated successfully, but these errors were encountered: