ios - How to convert json data from alamofire to swift objects -
hi there im making photo viewer app in swift using swift 1.1 in xcode 6.2
i having trouble trying convert json response alamofire swift objects.i have used swiftyjson library seems there compatibility issues.here model class
import foundation struct photo { var name: string var filename :string var notes: string }
here viewcontroller
import uikit class imageviewertableviewcontroller: uitableviewcontroller { var photos = [photo]() override func viewdidload() { super.viewdidload() alamofire.request(.get, "http://httpbin.org/get") .responsejson { (_, _, json, _) in } }
how can map json swift objects in situation
thanks .
the best solution use alamofireobjectmapper
.
your code should this:
import foundation import objectmapper struct photo: mappable { var name: string var filename :string var notes: string required init?(map: map) {} func mapping(map: map) { self.name <- map["name"] self.filename <- map["filename"] self. notes <- map["notes"] } }
in viewcontroller:
import uikit class imageviewertableviewcontroller: uitableviewcontroller { var photos = [photo]() override func viewdidload() { super.viewdidload() alamofire .request(.get, "http://httpbin.org/get") .responsearray { (response: response<[photo], nserror>) in if let myphotos = response.result.value { print(myphotos) } } } }
look documentation of alamofireobjectmapper
, objectmapper
more informations.
Comments
Post a Comment