FredLoh
FredLoh

Reputation: 1842

Dealing with nested for loops in Swift for a JSON file

One of the JSON requests that I make returns a file with a bunch of nested information. The format is roughly as follows groups->individual group->teams in group.

Currently I am dealing with this with a nested for loop where I look at the outer groups and then run the inner loop to get the information for the individual teams.

I've uploaded a copy of the JSON file to paste bin, here is the link. http://pastebin.com/D14wYDEs. This particular example doesn't have that many groups and teams but its possible to have way more, which makes the concept of nested for loops seem impractical.

I was wondering if somebody had a suggestion as to a better system of doing this, or any suggestion really.

Heres my current code:

func generateTablaDePosiciones() {
estadisticaUtilizada = 3
var tablaDePosicionesJSON = getJSONStats(3,tkn,eqID)

//checks to see that contents != nil, meaning the JSON file was found
if tablaDePosicionesJSON != nil {

    tablaDePosicionesArray.removeAll(keepCapacity: false)
    var numeroDeGruposEnTablaDePosiciones = tablaDePosicionesJSON["grupos"].count

    for var index = 0; index < numeroDeGruposEnTablaDePosiciones; ++index {

        var grupo = tablaDePosicionesJSON["grupos"][index]["grupo"].string
        var etiqueta1 = tablaDePosicionesJSON["grupos"][index]["etiqueta-1"].string
        var etiqueta2 = tablaDePosicionesJSON["grupos"][index]["etiqueta-2"].string
        var etiqueta3 = tablaDePosicionesJSON["grupos"][index]["etiqueta-3"].string
        var etiqueta4 = tablaDePosicionesJSON["grupos"][index]["etiqueta-4"].string
        var etiqueta5 = tablaDePosicionesJSON["grupos"][index]["etiqueta-5"].string
        var preTablaDePosicionesNuevo = preTablaDePosiciones(grupo: grupo!, etiqueta1: etiqueta1!, etiqueta2: etiqueta2!, etiqueta3: etiqueta3!, etiqueta4: etiqueta4!, etiqueta5: etiqueta5!)
        preTablaDePosicionesArray.append(preTablaDePosicionesNuevo)

        numeroDeTablaDePosiciones = tablaDePosicionesJSON["grupos"][index]["lista-body"].count

        for(var innerIndex = 0; innerIndex < numeroDeTablaDePosiciones; ++innerIndex) {

            var rank = tablaDePosicionesJSON["grupos"][index]["lista-body"][innerIndex]["rank"].string
            var equipoID = tablaDePosicionesJSON["grupos"][index]["lista-body"][innerIndex]["equipoID"].number! as Int
            var nomEquipo = tablaDePosicionesJSON["grupos"][index]["lista-body"][innerIndex]["nom-equipo"].string
            var d1 = tablaDePosicionesJSON["grupos"][index]["lista-body"][innerIndex]["d1"].string
            var d2 = tablaDePosicionesJSON["grupos"][index]["lista-body"][innerIndex]["d2"].string
            var d3 = tablaDePosicionesJSON["grupos"][index]["lista-body"][innerIndex]["d3"].string
            var d4 = tablaDePosicionesJSON["grupos"][index]["lista-body"][innerIndex]["d4"].string
            var d5 = tablaDePosicionesJSON["grupos"][index]["lista-body"][innerIndex]["d5"].string

            var tablaDePosicionesNuevo = tablaDePosiciones(rank: rank!, equipoID: equipoID, nomEquipo: nomEquipo!, d1: d1!, d2: d2!, d3: d3!, d4: d4!, d5: d5!)
                tablaDePosicionesArray.append(tablaDePosicionesNuevo)
        }
    }
} else {
    estadisticaUtilizada = 0
    println("Tabla de Posiciones JSON was nil")
}

}

Upvotes: 0

Views: 108

Answers (1)

Patrick Lynch
Patrick Lynch

Reputation: 2782

I would use a while loop. Increment an index with each execution and then dynamically construct the key using that index. Collect your results in an array and then pass on that array instead of each individual object.

Also, you should really be unwrapping all these values as you're parsing them instead of force unwrapping (!). In this while loop you can use a conditional binding while let to handle that, and if it fails—i.e. it found no value for that key—it will exit.

Something like this:

var index = 1
var results = [String]()
while let etiqueta = grupo["etiqueta-\(index)"] as? String {
    results.append( etiqueta )
    index++
}
let preTablaDePosicionesNuevo = preTablaDePosiciones(grupo: name, etiquetas: results)
preTablaDePosicionesArray.append( preTablaDePosicionesNuevo )

Upvotes: 1

Related Questions