Quick language: checking for zero, and if so, create a new object

Can this be simplified:

var unloadedImagesRows = [String:[Int]]() private func addToUnloadedImagesRow(row: Int, forLocation:String!) { if unloadedImagesRows[forLocation] == nil { unloadedImagesRows[forLocation] = [Int]() } unloadedImagesRows[forLocation]!.append(row) } 

Does Swift have an easy way to test nil , and if so, create a new object and all subsequent uses of it relate to the object?

+7
initialization swift
source share
6 answers

You can simplify it in only one line:

 private func addToUnloadedImagesRow(row: Int, forLocation:String!) { unloadedImagesRows[forLocation] = (unloadedImagesRows[forLocation] ?? []) + [row] } 
+8
source share

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" 
+2
source share

If you want to avoid if or guard , you can try nil coalescing operator (??) .

 private func addToUnloadedImagesRow(row: Int, forLocation:String!) { var rowsForLocation = unloadedImagesRows[forLocation] ?? [Int](); rowsForLocation.append(row) unloadedImagesRows[forLocation] = rowsForLocation } 

Note. This may not be very efficient as you need to re-assign the array in the dictionary. I'm not sure if this will lead to a full copy of the array.

+2
source share

you can use if let or guard statement

  private func addToUnloadedImagesRow(row: Int, forLocation:String!) { if let a = unloadedImagesRows[forLocation] as? [Int] { //...success block } } 

or use security instructions

  private func addToUnloadedImagesRow(row: Int, forLocation:String!) { guard let a = unloadedImagesRows[forLocation] else { return } //... } 

for more information. check link

+1
source share

You can check it as shown below.

 private func addToUnloadedImagesRow(row: Int, forLocation:String!) { if let image = unloadedImagesRows[forLocation] { //it is not nil } else { //it is nil } } 
+1
source share
 var unloadedImagesRows = [String:[Int]]() // if let private func addToUnloadedImagesRow(row: Int, forLocation:String!) { if let _ = unloadedImagesRows[forLocation] { } else { unloadedImagesRows[forLocation] = [Int]() } unloadedImagesRows[forLocation]!.append(row) } // guard let private func addToUnloadedImagesRow(row: Int, forLocation:String!) { guard let _ = unloadedImagesRows[forLocation] else { unloadedImagesRows[forLocation] = [Int]() return addToUnloadedImagesRow(row, forLocation: forLocation) } unloadedImagesRows[forLocation]!.append(row) } // nil coalescing private func addToUnloadedImagesRow(row: Int, forLocation:String!) { var b = unloadedImagesRows[forLocation] ?? [Int]() b.append(row) unloadedImagesRows[forLocation] = b } 
+1
source share

All Articles