Protocols declare methods that can be implemented by any class.
Formal Protocols
@protocol MyProtocol
- (void)requiredMethod;
@optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;
@required
- (void)anotherRequiredMethod;
@end
Informal Protocols
@interface NSObject ( MyXMLSupport )
- initFromXMLRepresentation:(NSXMLElement *)XMLElement;
- (NSXMLElement *)XMLRepresentation;
@end
Adopting a Protocol
@interface ClassName : ItsSuperclass < protocol list >
Categories adopt protocols in much the same way:
@interface ClassName ( CategoryName ) < protocol list >
@interface Formatter : NSObject < Formatting, Prettifying >
@end
0 评论: (+add yours?)
Post a Comment