使用Xcode 7.1
我正在尝试关注JSON并在tableView
中显示它我正在使用AlamofireObjectMapper将JSON转换为对象。
问题:我不知道如何在tableView中迭代对象
以下是自定义类,Alamofire Request&的TableView
类
class WeatherResponse: Mappable {
var location: String?
var threeDayForecast: [Forecast]?
required init?(_ map: Map){}
func mapping(map: Map) {
location <- map["location"]
threeDayForecast <- map["three_day_forecast"]
}
}
class Forecast: Mappable {
var day: String?
var temperature: Int?
var conditions: String?
required init?(_ map: Map){}
func mapping(map: Map) {
day <- map["day"]
temperature <- map["temperature"]
conditions <- map["conditions"]
}
}
Alamofire请求
//Defined within ViewController
var location: String?
var arrayOfForecasts = [Forecast]()
override func viewDidLoad() {
Alamofire.request(.GET, url).responseObject{(response :WeatherResponse?, error: ErrorType?)in
let location1 = response?.location
self.location = location1
let threeDayForecast1 = response?.threeDayForecast
self.arrayOfForecasts = threeDayForecast1!
print(threeDayForecast1)
self.tableView.reloadData()
} }
当我'打印(threeDayForecast1)'时,我得到以下输出: [MyAppName.Forecast,MyAppName.Forecast,MyAppName.Forecast]
TableView协议
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
let threeDayObj1 = arrayOfForecasts[indexPath.row]
cell.tempLabel?.text = threeDayObj1.conditions
return cell
}
我收到错误:
threeDayObj1 is array out of index
答案 0 :(得分:0)
在最新版本的AlamofireObjectMapper中,使用Swift 3,Xcode 8和iOS 10,您需要按照以下方式进行get调用:
Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).validate().responseObject { (response: DataResponse< WeatherResponse>) in
...