我的模型定义为
import ObjectMapper
class UserModel :Mappable{
var gender: String?
var name: String?
var location: String?
var email: String?
required init?(_ map: Map) {
}
// Mappable
func mapping(map: Map) {
gender <- map["gender"]
name <- map["name"]["title"]
location <- map["location"]["street"]
email <- map["email"]
}
}
我已经使用 Object Mapper 将 json 映射到 Model
var results:Mappable?
让 appUrl = " http://api.randomuser.me/?page=3&results=10 "
func requestRandomNamesFromServer(){
let randomPersonURl = appUrl
Alamofire.request(.GET, randomPersonURl, parameters: nil,headers:nil).responseJSON { response in
switch response.result {
case .Success(let data):
print(data)
self.results = Mapper<UserModel>().map(data)
print(self.results)
self.tableView.reloadData()
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
}