문자열이 포함되어 있는지 확인하기 위해 배열을 확인하는 구문으로 어려움을 겪고 있습니다. 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)
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다