1

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:

enter image description here

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);
Alfro
  • 1,524
  • 2
  • 21
  • 37
  • 1
    You have 2 `Array`s in this code. I'd narrow it down to which one is causing the issue by commenting out the TableData line and see if the code works. Also put a breakpoint on the `if let` line and see that all your properties and variables are initialized and have the data you expect in them. The problem is likely in the `jsonArrData as [JsonArrayValues]` if I had to guess. – DJohnson Jun 09 '16 at 00:58
  • you are right, when I comment this line of code there is no crash but I need this line of code. I've seted the values and seems they are coming okay, at least the first 30, so it has to crash nearly at the end. I've updated the post to add some code to show the variables declaration and usage – Alfro Jun 09 '16 at 06:39

1 Answers1

0

I managed to find out where the problem was, and It wasn't in the loop at all. I was accessing a global array from tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell file the array wasn't being filled anymore from that loop above, so it was crashing. Many thanks for the comments!

Alfro
  • 1,524
  • 2
  • 21
  • 37