Why does compactMap return a nil result?
问题 Consider this code snippet: var a: String? = "abc" var b: String? let result = [a, b].compactMap { $0 } After executing it, result will be ["abc"] which is the expected result. The element of result ( ElementOfResult ) here is String . print(type(of: result)) Array<String> Now to the interesting part. After changing the snippet to var a: String? = "abc" var b: Int? let result = [a, b].compactMap { $0 } and executing it, result will be [Optional("abc"), nil] The element of result (