I create one protocol method and I want to implement the protocol method in multiple classes
@protocol XMLProtocol <NSObject>
- (BOOL) hasAllRequiredAttributes :(NSDictionary*)attributeMap;
@end
I have use this following class methods
#import "XMLProtocol.h"
@interface MachineA : NSObject<XMLProtocol>
and its implementation method I will implement the protocol method
- (BOOL) hasAllRequiredAttributes:(NSDictionary *)attributeMap {
return false;
}
and also i use this protocol method in another class
#import "XMLProtocol.h"
@interface MachineB : NSObject<XMLProtocol>
and its implementation method I will implement the protocol method
- (BOOL) hasAllRequiredAttributes:(NSDictionary *)attributeMap {
return false;
}
my thought is where should I call the protocol method. I totally confused. How can i do this.
One way you can define a global implementation for your protocol method (if I understand correctly what you are asking) is defining a category on NSObject:
By doing like this every object will have that method. Don’t know if this is sensible, but it’s a way.
Another way would be defining a
Machinebase class from which bothMachineAandMachineBderive; the protocol method would be defined in the base class:and any derived class could redefine it, if required.
This is not as “global” as the NSObject category, but it might be a better solution if you can define a base class for all the classes that need implement that protocol.