问题:我试图用json数据创建一个自定义对象,但是当我向对象循环/添加数据时,swift正在给我一个错误的参数,但是在模型中我使用了一个可选的所以我很困惑问题出在哪里。最终这些数据将进入表格视图。
我的模型(使用选项):
class FlightDataModel {
var airline: String?
var price: String?
init(airline: String?, price: String?) {
self.airline = airline
self.price = price
}
}
alamofire API请求..
request(qpxRequest).responseJSON { (request, response, json, error) -> Void in
if response != nil {
//NSLog("%@", response!)
}
if json != nil {
if let myJSON = json as? [String:AnyObject] {
if let trips = myJSON["trips"] as? [String:AnyObject] {
if let data = trips["data"] as? [String:AnyObject] {
if let carriers = data["carrier"] as? [[String:String]] {
for (index, carrierName) in enumerate(carriers) {
// -----问题区域-------------
let myFlight = FlightDataModel(airline: carrierName["name"] as String)
self.arrayOfFlights[index] = myFlight
//println("\(self.arrayOfFlights[index].airline)")
}
}
}
}
}