Bound value in a conditional binding must be of Optional Type

前端 未结 5 784
你的背包
你的背包 2020-12-29 06:15

I have a protocol defined:

protocol Usable {
    func use()
}

and a class that conforms to that protocol

class Thing: Usabl         


        
5条回答
  •  长情又很酷
    2020-12-29 07:00

    You are getting

    Bound value in a conditional binding must be of Optional Type
    

    because thing as Usable must return an optional type so making it as? should solved the problem. Unfortunately, the error still persisted for some odd reason. Anyway, a workaround I found to get it to work is to extract out the variable assignment inside the if statement

    let thing = Thing()
    
    let usableThing = thing as? Usable
    
    if useableThing { 
        usableThing!.use()
    }
    else {
        println("can't use that")
    }
    

提交回复
热议问题