In Swift 3 I am no longer able to check whether generic variable type is class (AnyObject
) or not. Following code returns true
for isObject
even though specific type T
and passed value is struct and not class. In Swift 2.3 and 2.2 it works as expected and isObject
is false
.
struct Foo<T>
{
var value: Any?
var isObject: Bool = false
init (val: T?)
{
if val != nil
{
// following line shows warnings in Swift 3
// conditional cast from 'T?' to 'AnyObject' always succeeds
// 'is' cast is always true
isObject = val is AnyObject
self.value = val
}
}
}
struct Bar
{
var bar = 0
}
let b = Foo<Bar>(val: Bar())
print(b.isObject) // -> true
How can I make it work properly in Swift 3?