JonP JonP - 1 year ago 262
Swift Question

SwiftyJSON extremely slow JSON iteration

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

The code below is VERY slow, about one integration 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!)!

Answer Source

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!)!
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download