3

This SO post explains pretty well how to solve the issue of creating a delegate that is weak.

Essentially, there are 2 approaches:

Using the @objc keyword:

@objc protocol MyClassDelegate {
}

class MyClass {
  weak var delegate: MyClassDelegate?
}

Using the :class keyword:

protocol MyClassDelegate: class {
}

class MyClass {
  weak var delegate: MyClassDelegate?
}

I am trying to do some research to understand what exactly the differences between the two approaches are. The docs are pretty clear about using @objc:

To be accessible and usable in Objective-C, a Swift class must be a descendant of an Objective-C class or it must be marked @objc.

However, nowhere I found some information about what :class actually does. Considering the whole notion in detail, it actually doesn't make a lot of sense to. My understanding is that class is a keyword in Swift to declare classes. So, here it seems like we are using the keyword class itself as a protocol (as we're appending it after the : after the protocol declaration).

So, why does this even work syntactically and what exactly does it do?

Community
  • 1
  • 1
nburk
  • 22,409
  • 18
  • 87
  • 132
  • 1
    Related: [Swift: how to work around issue where weak variable of type 'protocol' is illegal](http://stackoverflow.com/questions/37132190/swift-how-to-work-around-issue-where-weak-variable-of-type-protocol-is-illega) – Hamish Oct 17 '16 at 21:34
  • 2
    Also related: [swift protocol 'weak' cannot be applied to non-class type](http://stackoverflow.com/q/33471858/2976878) – Hamish Oct 17 '16 at 21:36
  • 1
    Perhaps also interesting in this context: [What's the difference between a protocol extended from AnyObject and a class-only protocol?](http://stackoverflow.com/questions/30176814/whats-the-difference-between-a-protocol-extended-from-anyobject-and-a-class-onl) (and the answer *seems* to be "none"). – Martin R Oct 17 '16 at 21:38
  • 1
    @MartinR The first two linked Q&As look like suitable dupe targets to me, so should I vote to close for the first one, and then you dupe hammer with the second (that way both (I think) get linked)? – Hamish Oct 17 '16 at 21:42
  • I'm not sure. Both answer the question *"what :class actually does"*, but not the relation to `@objc`. – Martin R Oct 17 '16 at 21:45
  • 1
    @MartinR From what I gather from the question (could be wrong), OP understands `@objc` fine, he just doesn't get what `: class` does. I guess we can just wait for the OP to confirm in any case. – Hamish Oct 17 '16 at 21:45
  • yep, `@objc` is explained in the docs. I'm just struggling to understand the `: class` thing. even from a syntax standpoint it is weird to me that this even works. is that just like a special rule that we are allowed to use this keyword in protocol declarations? – nburk Oct 17 '16 at 21:48
  • 2
    Lookup "Class-Only Protocols" in the Swift book. – Martin R Oct 17 '16 at 21:49
  • that was exactly what I was looking for, just struggled to use the proper keywords this time as `class` and `protocol` only delivered very general hits on google... :/ – nburk Oct 17 '16 at 21:50
  • See [Class Only Protocols](https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID281). – Rob Oct 17 '16 at 21:50
  • 1
    @nburk Also see the [grammar section of the language guide](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/doc/uid/TP40014097-CH31-ID456) on inheritance clauses which details the specific case of `: class` (look for "class requirement") – Hamish Oct 17 '16 at 21:51

1 Answers1

5

:class ensures that only classes can implement the protocol. And that's any class, not just subclasses of NSObject. @objc, on the other hand, tells the compiler to use Objective-C-style message passing to call methods, instead of using a vtable to look up functions.

NRitH
  • 13,441
  • 4
  • 41
  • 44
  • thanks a lot for your answer! do you have any (official) resources explaining why we can use `: class` in a protocol declaration? the syntax seems pretty weird to me, really would like to read some more about this. – nburk Oct 17 '16 at 21:46
  • Do you mean just [Apple's Swift documentation](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID281)? – NRitH Oct 17 '16 at 21:48
  • oh yeah, that was pretty much what I was looking for, thanks!! – nburk Oct 17 '16 at 21:48