Bound value in a conditional binding must be of Optional Type

前端 未结 5 716
你的背包
你的背包 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 06:51

    This works for me in the playground

    protocol Usable {
        func use()
    }
    
    class Thing: Usable {
        func use () {
            println ("you use the thing")
        }
    }
    
    let thing = Thing()
    let testThing : AnyObject = thing as AnyObject
    
    if let otherThing = testThing as? Thing {
        otherThing.use()
    } else {
        println("can't use that")
    }
    

提交回复
热议问题