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?
Advertisements. Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. Protocols are implemented in the classes conforming to the protocol.
You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.
Structs do not inherit from protocols, they conform to protocols.
In iOS development, a protocol is a set of methods and properties that encapsulates a unit of functionality. The protocol doesn't actually contain any of the implementation for these things; it merely defines the required elements.
A protocol is just a list of method declarations. They can be class methods or instance methods. Example:
@protocol MyProtocol
+ (id) aClassMethod;
+ (void) someOtherClassMethod;
- (void) someInstanceMethod;
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With