0

Consider following example:

let a: Int! = 1
let b: Int! = 2
let array = [a, b]
print(type(of: array)) // prints: Array<Optional<Int>>

Why is inferred type of the array [Int?] and not [Int] or [Int!]? I thought implicit unwrapping deals with optional for good, at risk of runtime error. But in order to get non-optional [Int] type for the array, I need to do one of the following manually:

let array1 = [a!, b!]
let array2: [Int] = [a, b]
let array3 = [a, b] as [Int]
Gabriel Diez
  • 1,648
  • 2
  • 17
  • 26
Robo Robok
  • 21,132
  • 17
  • 68
  • 126

0 Answers0