json - why the error: fatal error: Array index out of range? [swift] -
please find error location in below image :
the number of values in tripoption change in each request.
there logic problem in code tripoption example outputs 2 values.. loop keeps going , says array out of index.. have no idea how fix issue.
var arrayofflights : [flightdatamodel] = [flightdatamodel]() if json != nil { //insert airline data arrayofflights 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]] { (index, carriername) in enumerate(carriers) { var myflight = flightdatamodel(airline: carriername["name"] string!, price:nil) self.arrayofflights.append(myflight) println("\(self.arrayofflights[index].airline!)") } } } if var tripoptions = trips["tripoption"] as? [[string:string]] { (index, tripoption) in enumerate(tripoptions) { self.arrayofflights[index].price = tripoption["saletotal"] string! println("price \(self.arrayofflights[index].price!)") } } } }
parameteers in url jsjon request:
var parameters = [ "request": [ "slice": [ [ "origin": from, "destination": to, "date": when ] ], "passengers": [ "adultcount": 1, "infantinlapcount": 0, "infantinseatcount": 0, "childcount": 0, "seniorcount": 0 ], "solutions": 5, "refundable": false ] ]
the error because trying access element in arrayofflights
index greater size - 1
.
Comments
Post a Comment