A protocol is the same thing as a Java interface. It just defines which methods the class should support. Here's a page that explains it clearly: http://www.otierney.net/objective-c.html#protocols
Essentially if you want to make sure a class will have a phoneNumber
method (accessor to the phoneNumber
property) you would do something like this:
@protocol ContactProtocol
-(void) phoneNumber;
@end
@interface Person: NSObject <ContactProtocol> {
...
}
@interface Company: NSObject <ContactProtocol> {
...
}
And then at compile time (or live for xcode 4) it will tell you if you forgot to add the phoneNumber
method to the Person
or Company
classes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…