user102008
user102008

Reputation: 31303

Is `AnyObject` special in the language?

By jumping to the definition of AnyObject in Xcode, it seems that AnyObject is declared like this:

@class_protocol protocol AnyObject {
}

However, if I try to declare my own protocol like this, it still doesn't work like AnyObject:

@class_protocol protocol Foo {
}
@objc class Bar {
}
var x : Foo = Bar() // compile error: Type 'Bar' does not conform to protocol 'Foo'
var y : AnyObject = Bar() // works

I've also tried @objc @class_protocol protocol Foo and it also doesn't work.


In addition, AnyObject has the property that you can call any method on it that was declared anywhere as an @objc method. Whereas for any other protocol, you can only call methods on it that the compiler knows the protocol supports:

@objc protocol Blah {
    func bogus()
}
@class_protocol protocol Foo {
}
class FooImpl : Foo {
}
var x : Foo = FooImpl()
var y : AnyObject = FooImpl()
x.bogus() // compile error
y.bogus() // works

So is AnyObject somehow special-cased in the language or something?

Upvotes: 1

Views: 625

Answers (2)

Gabriele Petronella
Gabriele Petronella

Reputation: 108101

It's not special in its declaration, it's special in the sense that every class implicitly conforms to AnyObject.

The only special thing here is the implicit part, so if you type

class Foo {}

the compiler will actually produce

class Foo: AnyObject {}

Again, there's is nothing special about the type system or the @class_protocol definition, as you could have achieved the same result by making every class to explicitly conform the AnyObject protocol.

Using the compiler to do this automatically, however, was the only sane solution to support legacy Objective-C code.

In order to make your example work, since you don't have the aid of the compiler, you would have to type

@class_protocol protocol Foo {}
@objc class Bar: Foo {}
var x : Foo = Bar()

Update

Well that wasn't exactly right. AnyObject is indeed special when referring to Objective-C compatibility, since you can call any Objective-c method on it and the compiler won't complain.

This is only true for Objective-C methods though.

In your last example, if you take the @objc keyword away, it gives a compile-time error on both calls.

protocol Blah {
    func bogus()
}
@class_protocol protocol Foo {
}
class FooImpl : Foo {
}
var x : Foo = FooImpl()
var y : AnyObject = FooImpl()
x.bogus() // compile error
y.bogus() // compile error

Upvotes: 1

Anton
Anton

Reputation: 559

Yes, it is. According to the official manual:

“AnyObject can represent an instance of any class type.”

Upvotes: 1

Related Questions