Search code examples
swiftanyobject

Why does SomeStruct() is AnyObject return true?


I'm a bit confused by the usage of AnyObject. Let me provide a few examples.

AnyObject

NSObject() is AnyObject

^ true (as expected)

class MyClass {}
MyClass() is AnyObject

^ true (as expected)

class MyClass {}
MyClass.self is AnyObject

^ true (as expected)

String() is AnyObject

^ true (as NOT expected)

struct MyStruct {}
MyStruct() is AnyObject

^ true (as NOT expected; String appears to be a struct)

String.self is AnyObject

^ false (as expected)

A snippet from Apple's documentation regarding AnyObject:

AnyObject can be used as the concrete type for an instance of any class, class type, or class-only protocol.

Why is an instance of struct is regarded as an AnyObject?


Solution

  • Ugh, this is one of my gripes with Swift. It's an Objective C interop feature, which although useful, is too implicit/mysterious. This implicit boxing behaviour only happens when Foundation is imported, and only on systems with ObjC support (Apple's platforms).

    Some Swift types bridge to specific ObjC counterparts, like NSString, NSNumber, NSArray, NSDictionary, etc. All other swift value types (like structs and tuples) are capable of being wrapped in a private ObjC class called _NSSwiftValue, which makes it possible to hand them off to ObjC APIs.

    The most confusing thing is that the object has an ill-defined identity (object address) as far as Objective C is concerned, and if your type doesn't conform to Hashable, then the Hash value of the object is also ill-defined, which can lead to all kinds of hard-to-nail-down bugs.