In Xcode8 beta6, the following code will cause a warning: 'is' test is always true. But it won't print pass.
struct TestStruct {
}
//warning: 'is' test is always true
if TestStruct() is AnyObject {
print("pass")
}
And the following code will cause a warning: Conditional cast from 'T' to 'AnyObject' always succeeds
public static func register<T>(_ protocolType: T.Type, observer: T) {
//Warning: Conditional cast from 'T' to 'AnyObject' always succeeds
guard let object = observer as? AnyObject else {
fatalError("expecting reference type but found value type: \(observer)")
}
//...
}
The warning works as intended: the
false
return ofTestStruct() is AnyObject
, however, does notThe prior version of this answer perceived the warning,
as the bug, and contained some discussion as to why this perceived buggy warning would manifest itself. That
TestStruct() is AnyObject
evaluated tofalse
at runtime, however, was perceived as expected behaviour.Given the comments to the bug report filed by the OP (SR-2420), it seems the situation is the reverse: since Xcode 8/beta 6, the
is
test should always evaluate totrue
, and the bug the OP:s post is the fact thatTestStruct() is AnyObject
evaluates tofalse
during runtime.Joe Groff writes:
The new
SwiftValue
box for conversion from Swift values to Obj-C objects(for additional details, see discussion in the comments below, thanks @MartinR)
It seems as if Swift values that are not explicitly implemented to be bridgeable to Obj-C objects via e.g. conformance to
_ObjectiveCBridgeable
(see e.g. the following Q&A for details regarding_ObjectiveCBridgeable
), will instead automatically make use of the newSwiftValue
box to allow conversion to Obj-C objects.The initial commit message for swift/stdlib/public/runtime/SwiftValue.mm reads:
Long story short.
To check if value has a reference type:
To check if type is a reference type:
More helpful answers: