You can create a helper statement to test nil and use it as shown below.
infix operator ?= { associativity left precedence 160 } func ?=<T: Any>(inout left: T?, right: T) -> T { if let left = left { return left } else { left = right return left! } }
Here you can use it as unloadedImagesRows[forLocation] ?= [Int]() if empty
var unloadedImagesRows = [String:[Int]]() private func addToUnloadedImagesRow(row: Int, forLocation:String!) { unloadedImagesRows[forLocation] ?= [Int]() unloadedImagesRows[forLocation]!.append(row) } addToUnloadedImagesRow(1, forLocation: "This is something") print(unloadedImagesRows) // "["This is something": [1]]\n"
Rahul katariya
source share