I am used to seeing things like id<NSCopying> myVar
or MyObject<NSCopying> myVar
, where we are stating that the variable in question can happily have NSCopying
methods called on it without the compiler throwing a wobbly.
But I recently spotted some code that defined a variable like this:
Class<NSCopying> myClass;
I was wondering what this actually means as it seems subtly different from the top two examples. It seems like we're saying that the variable myClass
can happily accept method calls from NSCopying
- but having a class type able to accept these instance variable methods doesn't seem to make much sense.
It has occurred to me that variables of type class are technically objects themselves which is probably confusing me or the compiler (probably me!).
So I guess I'm asking:
- What does something like
Class<NSCopying> myClass;
actually mean - How does
Class<NSCopying> myClass;
differ to something likeid<NSCopying> myVar
- Where could something like
Class<NSCopying> myClass;
be meaningfully used?
Notes:
- I am just using
NSCopying
as an example and isn't integral to my use case - Wherever I refer to
Class
I mean the Obj-C keywordClass
for declaring variables that are of typeClass
. I am not using this as a generic term for any class type.
Well I think it might mean that
Class <NSCopying> myClass
points to a class that implements NSCopying protocol. It can be useful if protocol has class method declarations and you want to call them.For example: