gpt4 book ai didi

arrays - 无法将类型 'String' 的值转换为预期的参数类型 '(Any) throws -> Bool'

转载 作者:行者123 更新时间:2023-11-28 10:49:00 24 4
gpt4 key购买 nike

我正在努力研究检查数组以查看它是否包含字符串的语法。我正在使用 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)
}

最佳答案

函数 contains(where:) 需要一个 closure 作为参数,你传递的是一个 String

因此,要修复您的代码,它应该是:

var connectedPeripherals = [String]()

if let connectedPeripherals = UserDefaults.standard.array(forKey: "ConnectedPeripherals") as? String {
if connectedPeripherals.contains(where: { $0 == peripheral.identifier.uuidString }) {
manager.connect(peripheral, options: nil)
}
}

关于arrays - 无法将类型 'String' 的值转换为预期的参数类型 '(Any) throws -> Bool',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47005406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com