Cannot invoke ‘!=’ with an argument List of type ‘(@lvalue (sucsess:
Bool!,products: [AnyObject]!) -> ()?,NilliteralConvertible)’ Type
‘(sucsess: Bool!,products: [AnyObject]!) -> ()?’ does not conform to
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!,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' } }}你需要在括号中包装你的闭包签名,使闭包本身是可选的。现在写的方式,闭包返回一个可选的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 }}总结
以上是内存溢出为你收集整理的swift – 可选闭包,并检查它是否为nil全部内容,希望文章能够帮你解决swift – 可选闭包,并检查它是否为nil所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)