我正在 xcode 6.2中使用swift 1.1快速制作照片查看器应用程序
hi there im making a photo viewer app in swift using swift 1.1 in xcode 6.2
我遇到了麻烦尝试将json响应从 alamofire转换为swift对象。我使用了swiftyjson库,但似乎存在兼容性问题。这是我的模型类
i am a having trouble trying to convert json response from alamofire to swift objects.i have used swiftyjson library but it seems there is a compatibility issues.here is my model class
import Foundation struct Photo { var name: String var filename :String var notes: String }
这是我的viewController
here is my viewController
import UIKit class ImageViewerTableViewController: UITableViewController { var photos = [Photo]() override func viewDidLoad() { super.viewDidLoad() Alamofire.request(.GET, "httpbin/get") .responseJSON { (_, _, JSON, _) in } }
在这种情况下,我如何将json映射为快速对象
how can i map json to swift objects in this situation
谢谢。
推荐答案最好的解决方案是使用 AlamofireObjectMapper 。
The best solution is to use AlamofireObjectMapper.
您的代码应该看起来像这样:
Your code should look like 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"] } }在viewController:
In viewController:
import UIKit class ImageViewerTableViewController: UITableViewController { var photos = [Photo]() override func viewDidLoad() { super.viewDidLoad() Alamofire .request(.GET, "httpbin/get") .responseArray { (response: Response<[Photo], NSError>) in if let myPhotos = response.result.value { print(myPhotos) } } } }AlamofireObjectMapper 和 ObjectMapper 的文档以获取更多信息。
Look the documentation of AlamofireObjectMapper and ObjectMapper for more informations.
更多推荐
如何将JSON数据从Alamofire转换为Swift对象
发布评论