所以我想要的是一个可以在函数中传递给它的闭包的类,它也可能在某个时候想要忽略那个闭包。如何检查闭包变量是否已设置,我可以在完成后删除它吗?
无法调用'!='使用类型'的参数列表(@lvalue(sucsess: Bool!,产品:[AnyObject]!) - > ()?,NilLiteralConvertible)'类型 '(sucsess:Bool!,产品:[AnyObject]!) - > ()&#39?;不符合 protocol' NilLiteralConvertible'
class someClass{
//typealias completionHandlerClosureType = (sucsess:Bool!, items:[AnyObject]!)->()
var completionHandler:(sucsess:Bool!, items:[AnyObject]!)->()?
var hitpoints = 100
var someset = ["oh no!","avenge me!"]
init(){}
func getHitFunc(impact:Int, passedCompletionsHandler:(sucsess:Bool!, items:[AnyObject]!)->()){
completionHandler = passedCompletionsHandler
hitpoints = hitpoints - impact
}
func checkIfDead{
if hitpoints<=0 { // The error received
if completionHandler != nil{// Cannot invoke '!=' with an argument list of type
//'(@lvalue (sucsess: Bool!, products: [AnyObject]!) -> ()?, NilLiteralConvertible)'
//run the handler if dead
completionHandler(sucsess: true, items: someset)
//do not run it again
completionHandler = nil //Type '(sucsess: Bool!, products: [AnyObject]!) -> ()?' does not conform to protocol 'NilLiteralConvertible'
}
}
else{
completionHandler = nil //Type '(sucsess: Bool!, products: [AnyObject]!) -> ()?' does not conform to protocol 'NilLiteralConvertible'
}
}
}
答案 0 :(得分:44)
您需要在括号中包含闭包签名,以使闭包本身可选。现在它的编写方式,闭包返回一个可选的Void(实际上没有意义)。
var completionHandler: ((sucsess:Bool!, items:[AnyObject]!)->())?
示例代码的一些样式点和修订:
// Capitalize class names so it's clear what's a class
class SomeClass {
// "success" has two "c"s
var completionHandler: ((success:Bool!, items:[AnyObject]!)->())?
var hitpoints = 100
var someset = ["oh no!","avenge me!"]
init() { }
func getHitFunc(impact:Int, passedCompletionsHandler:(success:Bool!, items:[AnyObject]!)->()){
completionHandler = passedCompletionsHandler
hitpoints = hitpoints - impact
}
// You were missing the argument list here:
func checkIfDead() {
if hitpoints <= 0 {
// Rather than checking to see if the completion handler exists, you can
// just call it using optional syntax like this:
completionHandler?(success: true, items: someset)
}
completionHandler = nil
}
}
答案 1 :(得分:42)
首先,在完成处理程序的声明中,您需要使用括号将整个事物声明为可选:
var completionHandler: ((_ success: Bool, _ items: [Any]?) -> ())?
或者,或许更好,您可以使用()
替换最终的Void
:
var completionHandler: ((_ success: Bool, _ items: [Any]?) -> Void)?
另外,请注意,我认为您不打算使Bool
成为可选项(因为如果闭包存在,您可能总是传递success
true
或{{}的值1}})。显然,false
数组可能是可选的。
无论如何,完成后,您只需确保解开该可选项:
items
当且仅当它不是func checkIfDead() {
if hitpoints <= 0 {
completionHandler?(true, items)
}
completionHandler = nil
}
时才会执行闭包,从而无需明确检查它是否为nil
。
对于它的价值,这可能是您nil
可能会减少混淆的一种情况:
typealias
那么财产就是:
typealias CompletionHandlerClosureType = (_ success: Bool, _ items: [Any]?) -> Void
将此var completionHandler: CompletionHandlerClosureType?
作为可选参数的函数可以执行:
completionHandler
然后最终完成逻辑不变:
func startSomeProcess(passedCompletionHandler: CompletionHandlerClosureType?) {
completionHandler = passedCompletionHandler
// do whatever else you want
}
(注意,以上内容已针对Swift 3进行了修改。如果您想查看Swift 2格式,请参阅此答案的previous revision。)