Published on

20250611

Authors
  • Name
    Twitter

发现了苹果官方文档的一处错误:

在 Swift -> Standard Library 章节中,在 Array 和 Dictionary 对实例方法 contains(where:)

https://developer.apple.com/documentation/swift/array/contains(where:)

https://developer.apple.com/documentation/swift/dictionary/contains(where:)

的代码示例上,把 dictionary 中的例子写成了 array 一样了:

enum HTTPResponse {
    case ok
    case error(Int)
}


let lastThreeResponses: [HTTPResponse] = [.ok, .ok, .error(404)]
let hadError = lastThreeResponses.contains { element in
    if case .error = element {
        return true
    } else {
        return false
    }
}
// 'hadError' == true

这是对 Array 的操作,并不是 Dictionary