I have a protocol, conforming class and a class with one simple function.
protocol Outputable {
static func output()
}
class Foo: Outputable {
static func output() {
print("output")
}
}
class Bar {
func eat(_ object: AnyObject?) {
if let object = object, let objectType = type(of: object) as? Outputable.Type {
objectType.output()
}
}
}
let foo = Foo()
let bar = Bar()
var fooOptional: Foo?
bar.eat(foo) // prints 'output'
bar.eat(fooOptional) // print nothing
Is there any way to pass some optional type being nil but conforming to Outputable
protocol and call protocol's static functions inside eat
function? Even though it's nil I am still passing a type and that's all I should need inside eat, right?
To make it more clear. I know why last line prints nothing. But is there a way to adjust eat
to print that 'output' string out?