迭代对象Swift - 在TableView中的索引 - AlamofireObjectMapper之外的数组

时间:2015-09-15 09:54:27

标签: ios swift uitableview alamofire

使用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

1 个答案:

答案 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
...