Non nil Arrays
1 2 |
var countryKeyArray : [String] = ["aha", "bar", "crab", "dolores", nil] print("\(countryKeyArray)") |
Array OF optionals
http://stackoverflow.com/questions/25589605/swift-shortcut-unwrapping-of-array-of-optionals
Notice ‘?’ is inside of the bracket, thus, depicting it as array of optionals
You can use nils in your array by using Optional types in your array. Just be reminded that you need to use flatMap to return an array of non-optionals and filter out any nils i.e arrayWithNoOptionals.
1 2 3 |
var countryKeyArray : [String?] = ["aha", "bar", "crab", "dolores", nil] let arrayWithNoOptionals = countryKeyArray.flatMap{ $0 } print("\(arrayWithNoOptionals)") |
Array that is optional
Notice question mark is outside of array type. Thus, marking it as an array that is optional. ile array itself has data or nil.
1 2 |
var countryKeyArray2 : [String]? = ["aha", "bar", "crab", "dolores"] countryKeyArray2 = nil |