I am a C# developer getting started on Objective-C / Cocoa Touch programming. I think I might have gotten some terms wrong because I keep thinking about them “the C# way”.
Specifically, I have come around the term “protocol” in various documentation and tutorials.
In Objective-C, what exactly is a protocol ? Can it be compared to a C# interface ?
Is the following declaration the same as saying “The class is implementing the protocol UITextFieldDelegate” ? Or is UITextFieldDelegate to be compared with a generic type parameter in C# ?
@interface MyViewController : UIViewController <UITextFieldDelegate> { }
In Objective-C a protocoll is the name for a collection of selectors/methods and is like an interface declaration in Java (probably also in C#).
means that the class
MyViewControllerinherits from the classUIViewControllerand adopts/implements the protocolUITextFieldDelegate.This means that
MyViewControllermust implement all methods declared in theUITextFieldDelegate.EDIT: It seems that with the introduction of Objective-C 2.0 the possibility to mark methods of a protocol as
@optionaland@requiredwas introduced.See section
Optional Protocol Methodsof Apples Objective-C documentation.Helpful link from wikibooks about Objective-C Protocols.