I got a simple loop, in which I want to append a variable to an array if it exists. Every variable is checked... Anybody knows why this is crashing?
This is the error I get:
NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray
This is the first loop where the arrays are declared and used:
private func parseJson(json : NSMutableArray, tableView : UITableView){
var jsonArrData : [JsonArrayValues] = []
var c : Int = 0
for j in json {
var jsonValues : JsonArrayValues = JsonArrayValues()
//Create main value
guard let value = j.valueForKey("value")?.valueForKey("value")! else{
continue
}
//Get name
guard let Name : String = (value.valueForKey("Name")?.valueForKey("en") as? String) else {
continue
}
jsonValues.name = Name
jsonArrData.append(jsonValues)
c += 1
}
//Reorder Array by shop distance from user...
jsonArrData.sortInPlace({$0.distance < $1.distance})
//This is the loop that crashes:
for jsonVal in jsonArrData as [JsonArrayValues]{
if let name : String = jsonVal.name where jsonVal.name.isEmpty == false{
TableData.append(name)
}
}
}
This is the class jsonValues:
import Foundation
class JsonArrayValues {
init(){
}
var name : String = ""
var distance = Float()
var lat = Float()
var lng = Float()
var openingTime = String()
var country = String()
var code = String()
var address = String()
var categories = String()
var city = String()
var type = String()
var brands = String()
}
edit: I added breakpoints and changed a bit the code to be able to test better and looks like is not crashing in the loop:
As you can see in the image, i've set the index starting at 10 because I was thinking the app was crashing in the last index in the loop. The array It's in the last index, and the loop breaks, but the app continues the execution and crashes righ after this line:
doTableRefresh(tableView);