简体   繁体   中英

Get max and average values from an array of dictionaries swift

I have an array (filled by NSUserDefaults: Defaults.arrayForKey("onLine") as! [[String:AnyObject]]) and how can I get the average and max values from SPEED?

I Tried online.max!["SPEED"] but didn't work

Any helps?

[["LONGITUDE: ": -122.02211045, "BEARING: ": 263.41, "LATITUDE: ": 37.32475565, "SPEED: ": 3.76, "TIMESTAMP: ": 2016-10-19T02:09:49.535Z], ["LONGITUDE: ": -122.02230437, "BEARING: ": 270.15, "LATITUDE: ": 37.32475499, "SPEED: ": 3.15, "TIMESTAMP: ": 2016-10-19T02:09:54.518Z], ["LONGITUDE: ": -122.02249983, "BEARING: ": 265.56, "LATITUDE: ": 37.32475587, "SPEED: ": 3.27, "TIMESTAMP: ": 2016-10-19T02:09:59.516Z]]

I fixed it my self with this code

speedArray.append(userLocation.speed * 1.94384)
        let avgSpeed = speedArray.reduce(0, combine: +) / Double(speedArray.count)
        let maxSpeed = speedArray.maxElement()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM