Reputation: 1128
Swift3. I am wondering how exactly UICollectionViewDelegate
can be declared as a weak property.
If you check swift 'headers' you will see that it inherits from UIScrollViewDelegate
which in turn inherits from NSObjectProtocol
which is not marked as class
-only protocol.
If I try to make my own protocol to use with delegate pattern as weak
'delegate' property I get compile time error 'weak' may only be applied to class and class-bound protocol types
.
While I understand why compiler forces me to use class
, I am wondering how UICollectionViewDelegate
can get away with this. I would guess class
is some kind of syntactic sugar but I would appreciate if you could explain me.
On a side note: Is there a pattern to deal with situations where I really need a protocol which could be adopted by both value and reference types but also "easily" managed from memory point of view (meaning weak
references).
I am sorry for a bit fuzzy question.
Upvotes: 0
Views: 124
Reputation: 54630
When you use the class
keyword on a protocol
definition, you're saying "this protocol only applies to classes, not Swift structs or enums."
When an object inherits from, or is, an Objective-C class or protocol, it by definition isn't a Swift struct or enum. So it doesn't need to specify.
Upvotes: 2