gpt4 book ai didi

快速关闭不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 07:21:13 25 4
gpt4 key购买 nike

我正在尝试运行闭包但收到错误消息。无法将类型“()”的值转换为闭包结果类型“Bool”。我不确定我在这里做错了什么。

func runPrintCycle(){
self.runPrinter1() { success in
self.runPrinter2() { success in
print("Success")
}
}
}

func runPrinter1(completionHandler: (Bool) -> Bool){
if let printer1 = Workstation.instance.printer1{
let receiptPrinter = Print(printer: printer1)
receiptPrinter.runPrinterReceiptSequence() { success in
completionHandler(true)
}
}else{
completionHandler(true)
}
}

func runPrinter2(completionHandler: (Bool) -> Bool){
if let printer2 = Workstation.instance.printer2{
let receiptPrinter = Print(printer: printer2)
receiptPrinter.runPrinterReceiptSequence() { success in
completionHandler(true)
}
}else{
completionHandler(true)
}
}

最佳答案

您可能不需要在 runPrinter 函数中将 completeon 闭包声明为返回的 Bool 值。让它们返回 Void。当找不到打印机时,您可能还想在关闭时发送 false:

func runPrintCycle() {
self.runPrinter1() { success in
print("Printer 1: \(success)")
// put here if(success) if you wish run second printer only on success
self.runPrinter2() { success in
print("Printer 2: \(success)")
}
}
}

func runPrinter1(completionHandler: (Bool) -> ()) {
if let printer1 = Workstation.instance.printer1 {
let receiptPrinter = Print(printer: printer1)
receiptPrinter.runPrinterReceiptSequence() { success in
completionHandler(true) //probably success instead true?
}
}else{
completionHandler(false)
}
}

func runPrinter2(completionHandler: (Bool) -> ()){
if let printer2 = Workstation.instance.printer2{
let receiptPrinter = Print(printer: printer2)
receiptPrinter.runPrinterReceiptSequence() { success in
completionHandler(true) //probably success instead true?
}
}else{
completionHandler(false)
}
}

关于快速关闭不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38859718/

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