JonP JonP - 9 months ago 61
Swift Question

swift extremely slow json interation

I am using a lib json.swift. I successfully import a valid json file but it's very large (500kb) (actully a gpx-file from Strava)

The code below is VERY slow, about one interation per second! Why? And what can I do about it?

for index in 0...json1["trk"]["trkseg"]["trkpt"].length-1 {

lat = Double(json1["trk"]["trkseg"]["trkpt"][index]["@attributes"]["lat"].asString!)!

long = Double(json1["trk"]["trkseg"]["trkpt"][index]["@attributes"]["lon"].asString!)!


I think the following change could speed up your code a bit:

let array = json1["trk"]["trkseg"]["trkpt"]
for bla in array {
    lat = Double(bla["@attributes"]["lat"].asString!)!
    long = Double(bla["@attributes"]["lon"].asString!)!