gpt4 book ai didi

ios - Swift 编写一个带有返回值的 async/await 方法

转载 作者:行者123 更新时间:2023-11-29 05:28:16 24 4
gpt4 key购买 nike

我想编写一个带有返回值的异步等待方法,但我的代码不起作用。我还尝试了另一种方式例如DispatchQueue.global DispatchGroup()等。
这是我的代码:

func checkPassCode() -> Bool {

var result = false

let closure = { (_ flag:Bool) -> Void in
result = flag
}

if var pin = self.keychain.get("pin") {
let userPin = self.pin.joined(separator: "")
let encryptedData = NSData(base64Encoded: pin, options: [])

AsymmetricCryptoManager.sharedInstance.decryptMessageWithPrivateKey(encryptedData! as Data) { (success, result, error) -> Void in
if success {
pin = result!
print("userPin is: \(userPin)")
print("storePin is: \(pin)")
closure(userPin == pin)
} else {
print("Error decoding base64 string: \(String(describing: error))")
closure(false)
}
}
}
return result
}

最佳答案

谢谢,vadian评论。我使用闭包作为该方法的输入参数。

// MARK: - PassCode Methods
func checkPassCode(completionHandler:@escaping (_ flag:Bool) -> ()) {
let storePin = getStorePin()
let userPin = self.pin.joined(separator: "")
AsymmetricCryptoManager.sharedInstance.decryptMessageWithPrivateKey(storePin as Data) { (success, result, error) -> Void in
if success {
let pin = result!
print("userPin is: \(userPin)")
print("storePin is: \(pin)")
completionHandler(userPin == pin)
} else {
print("Error decoding base64 string: \(String(describing: error))")
completionHandler(false)
}
}
}

func getStorePin() -> NSData {
if let pin = self.keychain.get("pin") {
return NSData(base64Encoded: pin, options: []) ?? NSData()
}
return NSData()
}

然后调用这个方法:

checkPassCode { success in
if success {
print("sucess")
} else {
print("not sucess!")
}
}

关于ios - Swift 编写一个带有返回值的 async/await 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57935483/

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