Collection

  • To iterate an array with Struct You cannot change property of struct in loop. During the loop, you are operate the copy of the struct. Old loop method should be used for it.
for index in 0..<nearbyTrolleys.count {
    nearbyTrolleys[index].distance = nearbyTrolleys[index].location.distance(from: currentlocation)
}
  • Check item exist in array
var b:[String] = ["a", "b", "c", "d", "e"]
b.contains("a")
  • Filter item in array
var a = [1, 2, 2, 3, 4, 5]
let a3 = a.filter{$0 == 2}
print(a3)
  • Sort array
var a = [1, 2, 2, 3, 4, 5]
let d = a.sorted(by: >)
  • Map
let a4 = [1,3,5,7]
let b4 = a4.map { (item:Int) -> Int in
    return item * 2
}
  • Reduce
let status = [true, true, true, true]
var s1 = status.reduce(true) { (r, s) -> Bool in
  return r && s
}
  • To check an item exist in Array
if Array(sensorsid.keys).contains(messageid){
              processSensorMessage(json: json, index: messageid)
}
  • To find the index of element
let arr = ["a", "b", "c"]
let indexofA = arr.firstIndex("a")