【发布时间】:2017-10-29 21:27:45
【问题描述】:
我正在努力检查数组以查看它是否包含字符串的语法。我正在使用 contains 函数检查字符串数组。但是注意到错误,我无法计算出闭包的语法。任何人都可以帮忙吗?
var connectedPeripherals = [String]()
if let connectedPeripherals = UserDefaults.standard.array(forKey: "ConnectedPeripherals") {
if connectedPeripherals.contains(where: (peripheral.identifier.uuidString)) {
// Gives error: "Cannot convert value of type 'String' to expected argument type '(Any) throws -> Bool'"
manager.connect(peripheral, options: nil)
}
}
【问题讨论】:
-
您应该使用 UserDefaults 方法
stringArray(forKey:)developer.apple.com/documentation/foundation/userdefaults/… 返回一个字符串数组[String]。顺便说一句,使用if let connectedPeripherals = ...不会为您在条件闭包之外声明的变量分配任何值。
标签: arrays swift string closures