Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 快速关闭不起作用_Swift - Fatal编程技术网

Swift 快速关闭不起作用

Swift 快速关闭不起作用,swift,Swift,我试图运行关闭,但收到一个错误。无法将类型“()”的值转换为闭包结果类型“Bool”。我不确定我在这里做错了什么 func runPrintCycle(){ self.runPrinter1() { success in self.runPrinter2() { success in print("Success") } } } func runPrinter1(comp

我试图运行关闭,但收到一个错误。无法将类型“()”的值转换为闭包结果类型“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)
    }
}