My guess for the first is that the compiler in the first performs type inference and analyses ["hello", "world"] as array of optional.
let result = ["hello", "world"].contains(Optional("hello")) // ✅ Works fine
But in the second,
let stringArray = ["hello", "world"]
let result = stringArray.contains(Optional("hello")) // ❌ Compile-time error
you explicitly declare stringArray to be [String].
Then in second line, compiler balks at it.
Topic:
Programming Languages
SubTopic:
Swift
Tags: