Swift 将完成处理程序添加到框架';s用于响应处理的委托函数(爱普生打印机)
因此,我有一个处理打印作业队列的逻辑(这是处理所有其他打印机集成的方式),该队列为执行打印作业的每个打印机调用特定函数(在本例中为sendCommand函数),然后报告成功/失败,以便我们能够正确处理重试或删除打印作业文档Swift 将完成处理程序添加到框架';s用于响应处理的委托函数(爱普生打印机),swift,printing,epos,Swift,Printing,Epos,因此,我有一个处理打印作业队列的逻辑(这是处理所有其他打印机集成的方式),该队列为执行打印作业的每个打印机调用特定函数(在本例中为sendCommand函数),然后报告成功/失败,以便我们能够正确处理重试或删除打印作业文档 func sendCommand(_ commands: Data, portName: String, portSettings: String, timeoutMilis: uint_least32_t) -> Bool { doPrinterSetup()
func sendCommand(_ commands: Data, portName: String, portSettings: String, timeoutMilis: uint_least32_t) -> Bool {
doPrinterSetup()
let succPrint = cutAndCompletePrintJob(commands)
if (connectedPrinter != nil) {
disconnectPrinter()
}
while (!hasCompleted) { //Awful solution?
sleep(1)
}
return printSuccess
}
在打印机的特定逻辑中,我建立连接,构建打印数据,等等。。。那就寄吧。我得到的响应是命令已成功发送
func printData(_ data: Data) -> Bool {
if connectedPrinter == nil {
return false
}
connectedPrinter!.addCommand(data) //Add print information
connectedPrinter!.addCut(EPOS2_CUT_FEED.rawValue) //Add cut
connectedPrinter!.beginTransaction()
connectedPrinter!.sendData(Int(EPOS2_PARAM_DEFAULT))
return true
}
EPOS框架有一个单独的委托函数,onPtrReceive,在打印作业实际完成打印后调用,如下所示,它实际报告打印作业的成功或失败:
//EPOS Delegate func
func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {
hasCompleted = true
print("You could use closure for this problem.
add a closure parameter to the sendCommand
method and remove the Bool return value, hold this parameter as you class property.
// Bool stand for the status of this printing job, true means success.
var completion: ((Bool) -> Void)?
func sendCommand(_ commands: Data, portName: String, portSettings: String, timeoutMilis: uint_least32_t, completion: @escaping (Bool -> Void)?) {
self.completion = completion
// ....
}
//EPOS委托函数
func onPtrReceive(printerObj:Epos2Printer!,代码:Int32,状态:Epos2PrinterStatusInfo!,printJobId:String!){
hasCompleted=true
打印(“您可以使用闭包解决此问题
将闭包参数添加到sendCommand
方法并删除Bool返回值,将此参数作为类属性保留
//Bool表示此打印作业的状态,true表示成功。
var完成:((Bool)->无效)?
func sendCommand(u命令:数据,端口名称:字符串,端口设置:字符串,timeoutMilis:uint_least32_t,完成:@escaping(Bool->Void)?){
自我完成=完成
// ....
}
在委托方法中调用此闭包
func onPtrReceive(printerObj:Epos2Printer!,代码:Int32,状态:Epos2PrinterStatusInfo!,printJobId:String!){
让success=isPrintJobSuccess(by:code)//确定作业如何成功
自我完成?(成功)
}
假设您的类名为Printer
,您可以在调用站点上执行此操作:
let printer=printer()
printer.sendData(cmdData,端口名:“port”,端口设置:“settings”,timeoutMilis:1000,完成:{success in
如果成功{
doSomeSuccessJob()
}
})
//或者你可以使用尾随闭包
printer.sendData(cmdData,端口名:“port”,端口设置:“settings”,timeoutMilis:1000){success in
如果成功{
doSomeSuccessJob()
}
}
参考文献:这回答了您的问题吗?不知道您可以这样使用闭包。这回答了问题,谢谢!