ObjC: is there such a thing as a "class protocol"?
objective-c
Solution
There are such things as Protocols for class methods, and they're called.... Protocols. For example, it looks like you want a protocol that looks like this:
@protocol MyDecoder
+ (id)decode:(SExp *)root;
@end
You can then use it like this:
Class c = [self modelClass:kind];
if ([c conformsToProtocol:@protocol(MyDecoder)]) {
model = [c decode: [SExpIO read: [fm contentsAtPath:target]]];
}
Problem
For object instances we can have their class declare some protocol conformance as in: ``` @protocol P <NSObject> - (void) someMethod ; @end @interface C : NSObject <P> @end @implementation C - (void) someMethod { } @end ``` But what about classes? I find myself in this situation: ``` ... Class c = [self modelClass:kind] ; if (c) { model = [c performSelector: @selector(decode:) withObject: [SExpIO read: [fm contentsAtPath:target]]] ; } ``` and I wish there were a way for me to declare that there is such a thing as protocols for class methods. In the above example, all classes that c can be a class-instance (Hmmm??) of, declare ``` + (id) decode: (SExp *) root ; ``` Is there a way that I could transform the above into: ``` if (c) { model = [c decode: [SExpIO read: [fm contentsAtPath:target]]] } ``` by using a suitable "class protocol" declaration?