[英]Swift Closures - How to pass a parameter and have no return value
In the following code, I'm trying to create a closure that is passed a single parameter that will be used in the closure, where no value is returned: 在下面的代码中,我尝试创建一个闭包,该闭包将传递一个将在闭包中使用的参数,其中不返回任何值:
The code is called from another Swift class in my app via runit()
该代码是通过
runit()
从我的应用程序中的另一个Swift类调用的
Cannot invoke value of type '(CheckerOperation) -> ()' with argument list '(CheckerOperation)' at the line “runTimerProcess(runitProcess(customOperation))
无法在“ runTimerProcess(runitProcess(customOperation))”行中使用参数列表“(CheckerOperation)”调用类型为“(CheckerOperation)->()”的值
I don't understand how to invoke when the closure will not be returning a value. 我不明白当闭包不返回值时如何调用。
( CheckerOperation
is just a custom NSOperation
class) (
CheckerOperation
只是一个自定义的NSOperation
类)
class Checker {
var queue = NSOperationQueue()
let customOperation : CheckerOperation = CheckerOperation()
var runitProcess: (CheckerOperation) -> () = {op in
NSOperationQueue.mainQueue().addOperationWithBlock({
let operationQueue = NSOperationQueue.mainQueue()
operationQueue.addOperation(op)
})
}
func runTimerProcess(closureblock: ClosureBlock){
let queue = dispatch_queue_create(“myqueue”, DISPATCH_QUEUE_SERIAL)
dispatch_async(queue,closureblock)
}
func runit(){
runTimerProcess(runitProcess(customOperation))
}
}
You're mixing two core syntaxes: (1) computed properties , and (2) functions/methods . 您正在混合两种核心语法:(1) 计算属性和(2) 函数/方法 。
Computed properties cannot accept arguments. 计算属性不能接受参数。 For your case, you should be simply defining a function with an argument
op
with a type of CheckerOperation
. 对于您的情况,您应该仅使用类型为
CheckerOperation
的参数op
定义函数。 I say this because you don't appear to need to return or check a value on runitProcess
which is really what a computed property is intended for. 我说这是因为您似乎不需要返回或检查
runitProcess
上的值,这实际上是计算属性的用途。
Use this: 用这个:
func runitProcess(op: CheckerOperation) {
NSOperationQueue.mainQueue().addOperationWithBlock({
let operationQueue = NSOperationQueue.mainQueue()
operationQueue.addOperation(op)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.