let test = [4, 5, 3, 1, 3]
print(
test.map { $0 }
)
print(
test.map(\.self) // Doesn't compile
)
错误:
在没有更多上下文的情况下,表达式的类型是模棱两可的
为什么它不起作用?似乎应该如此。
如果不是这样,我们还能如何摆脱这里丑陋的 { $0 } 呢?
也许compactMap的例子会更香))
let test = [4, 5, nil, 3, 1, nil, 3]
print(
test.compactMap { $0 }
)
print(
test.compactMap(\.self) // Doesn't compile
)
错误:
无法将 'WritableKeyPath<_, _>' 类型的值转换为预期的参数类型 '(Int?) throws -> ElementOfResult?'