# Class-only protocols, class vs AnyObject?

**URL:** <https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507>\
**Category:** Using Swift\
**Created:** [March 30, 2018, 7:41am UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507 "2018-03-30T07:41:42Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [March 30, 2018, 7:41am UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/1 "2018-03-30T07:41:42Z")

</div>

What is the difference, if any, btw P and Q here:

```
protocol P : class { ... }

protocol Q : AnyObject { ... }

```

If no difference, which should be preferred?

(I got the impression that the class version was meant to be deprecated, but it still compiles without warnings as of Xcode 9.3)

---

<div class="post-metadata">

**Author:** ![tkrajacic](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkrajacic/32/1173_2.png) [@tkrajacic](https://forums.swift.org/u/tkrajacic)\
**Post date:** [March 30, 2018, 8:21am UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/2 "2018-03-30T08:21:41Z")

</div>

See [swift - What's the difference between a protocol extended from AnyObject and a class-only protocol? - Stack Overflow](https://stackoverflow.com/questions/30176814/whats-the-difference-between-a-protocol-extended-from-anyobject-and-a-class-onl/32895975#32895975) for a subtle difference, but yes it has been stated that it will be removed. I also don't know why it wasn't deprecated in this release.

---

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [March 30, 2018, 8:54am UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/3 "2018-03-30T08:54:54Z")

</div>

Thanks, If the following (from @codafi's stack overflow answer) is true:

> The difference lies in the semantics of @objc declarations. With AnyObject, the expectation is that conforming classes may or may not be proper Objective-C objects, but the language treats them as such anyway (in that you lose static dispatch sometimes).

Does this mean that inheriting from `AnyObject` might incur a performance overhead (compared to if `class` had been used)?

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [March 30, 2018, 9:06am UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/4 "2018-03-30T09:06:16Z")

</div>

The StackOverflow answer is out of date as of Swift 4. `protocol P : class {}` and `protocol P : AnyObject {}` parse identically and there's no semantic difference between them. We should add a warning to deprecate the former at some point.

---

<div class="post-metadata">

**Author:** ![fabb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fabb/32/2230_2.png) [@fabb](https://forums.swift.org/u/fabb)\
**Post date:** [June 13, 2018, 9:01am UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/5 "2018-06-13T09:01:44Z")

</div>

> The StackOverflow answer is out of date as of Swift 4. protocol P : class {} and protocol P : AnyObject {} parse identically and there’s no semantic difference between them. We should add a warning to deprecate the former at some point.

But even @jrose prefers `class` in his WWDC 2018 talk 😜

 ![IMG_1956](https://global.discourse-cdn.com/swift/original/2X/6/672743825c715c6801b588886eeaa2a9701f0487.jpg)

---

<div class="post-metadata">

**Author:** ![Tino](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tino/32/1073_2.png) [@Tino](https://forums.swift.org/u/Tino)\
**Post date:** [June 13, 2018, 9:06am UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/6 "2018-06-13T09:06:33Z")

</div>

It's just fair that Swift Evolution not only breaks users code, but also creators slides ;-)

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [June 13, 2018, 4:21pm UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/7 "2018-06-13T16:21:01Z")

</div>

Oof, my bad. Should have had Slava review them first!

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [June 13, 2018, 5:44pm UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/8 "2018-06-13T17:44:34Z")

</div>

Don't worry, you weren't the only presenter with imperfect Swift code. I'm pretty sure I remember a session where the presenter typed out `-> Void` return types. It's fairly clear that many Apple's still aren't that familiar with Swift. For some, I'm guessing WWDC slides and presentations may be the only time they write Swift at all. At least I haven't noticed any semicolons this year!

---

<div class="post-metadata">

**Author:** ![Vyacheslav](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@Vyacheslav](https://forums.swift.org/u/Vyacheslav)\
**Post date:** [July 24, 2018, 4:24pm UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/9 "2018-07-24T16:24:55Z")

</div>

Which keyword is preferred? Assumed that seems 'class' behaves incorrectly, is AnyObject better? Will class be deprecated?

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 24, 2018, 4:42pm UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/10 "2018-07-24T16:42:08Z")

</div>

Both are the same, `class` is deprecated in favor of `AnyObject` simply without a warning. See Slavas response above.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [July 25, 2018, 9:12pm UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/11 "2018-07-25T21:12:29Z")

</div>

> [@Vyacheslav](#):
>
> Which keyword is preferred? Assumed that seems 'class' behaves incorrectly, is AnyObject better? Will class be deprecated?

Both keywords behave identically. AnyObject is preferred because it is the name of a type and can appear anywhere that a type can. "class" is just special syntax in that one place in a protocol's inheritance clause, and it parses identically to "AnyObject".

---

<div class="post-metadata">

**Author:** ![smashkins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/smashkins/32/736_2.png) [@smashkins](https://forums.swift.org/u/smashkins)\
**Post date:** [February 6, 2019, 6:53pm UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/12 "2019-02-06T18:53:59Z")

</div>

Quoting Matt Neuburg:

> [@](#):
>
> A protocol declared with the keyword class after the colon after its name is a class protocol, meaning that it can be adopted only by class object types:
> 
> ```
> protocol SecondViewControllerDelegate : class {
> func accept(data:Any!)
> 
> ```
> 
> }  
> (There is no need to say class if this protocol is already marked @objc; the @objc attribute implies that this is also a class protocol, because classes are the only Objective-C object type.)  
> A typical reason for declaring a class protocol is to take advantage of special memory management features that apply only to classes
> 
> class SecondViewController : UIViewController {  
> weak var delegate : SecondViewControllerDelegate?  
> // ...  
> }  
> The keyword weak marks the delegate property as having special memory management that applies only to class instances. The delegate property is typed as a protocol, and a protocol might be adopted by a struct or an enum type. So to satisfy the compiler that this object will in fact be a class instance, and not a struct or enum instance, the protocol is declared as a class protocol.  
> In Swift 4 or later, you might alternatively take advantage of classprotocol composition to accomplish the same thing:
> 
> ```
> class SecondViewController : UIViewController {
> weak var delegate : (NSObject & SecondViewControllerDelegate)?
> // ...
> 
> ```
> 
> }  
> That's legal even if SecondViewControllerDelegate has no class designation, because the compiler knows that the object assigned to the delegate property will derive from NSObject, in which case it is a class instance.

But... the 0156 proposal says:

> This proposal merges the concepts of `class` and `AnyObject` , which now have the same meaning: they represent an existential for classes. To get rid of the duplication, we suggest only keeping `AnyObject` around. To reduce source-breakage to a minimum, `class` could be redefined as `typealias class = AnyObject` and give a deprecation warning on `class` for the first version of Swift this proposal is implemented in. Later, `class` could be removed in a subsequent version of Swift.

[0156 proposal](https://github.com/apple/swift-evolution/blob/master/proposals/0156-subclass-existentials.md)

---

<div class="post-metadata">

**Author:** ![pepejeria](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pepejeria/32/13704_2.png) [@pepejeria](https://forums.swift.org/u/pepejeria)\
**Post date:** [November 6, 2020, 8:57am UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/13 "2020-11-06T08:57:38Z")

</div>

Is this information still valid? Should we avoid using class and use AnyObject instead?

---

<div class="post-metadata">

**Author:** ![donny\_wals](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/donny_wals/32/24944_2.png) [@donny\_wals](https://forums.swift.org/u/donny_wals)\
**Post date:** [November 6, 2020, 9:13am UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/14 "2020-11-06T09:13:51Z")

</div>

Yes

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [November 17, 2020, 2:04am UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/15 "2020-11-17T02:04:31Z")

</div>

You can use either one, but I suggest using AnyObject since it can be used in more places (such as protocol compositions, eg `MyProto & AnyObject`). Also I’d like us to add a warning that `class` is deprecated at some point (this would make for a great starter bug if anyone is interested).

---

<div class="post-metadata">

**Author:** ![wristbands](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@wristbands](https://forums.swift.org/u/wristbands)\
**Post date:** [July 31, 2023, 9:43pm UTC](https://forums.swift.org/t/class-only-protocols-class-vs-anyobject/11507/16 "2023-07-31T21:43:39Z")

</div>

As of today, `class` is now officially deprecated in favor of `AnyObject`.
