Search code examples
objective-cmacoscocoaobjc-protocol

What are the angle brackets "<…>" in an Obj-C class interface for?


Can anyone tell me what the angle brackets <...> in an Objective-C class interface do? Like this one (from http://snipt.net/robhawkes/cocoa-class-interface):

@interface MapMeViewController : UIViewController <CLLocationManagerDelegate, 
            MKReverseGeocoderDelegate, MKMapViewDelegate, UIAlertViewDelegate> { ... }

From my view they look like some sort of type declaration (considering my previous experience in PHP and JavaScript), like we're making sure MapMeViewController is a CLLocationManagerDelegate, MKReverseGeocoderDelegate, MKMapViewDelegate, or UIAlertViewDelegate

Documentation about the @interface syntax don't seem to mention this.


Solution

  • The angle brackets in a class interface definition indicates the protocols that your class is conforming to.

    A protocol is almost like an interface in Java or C#, with the addition that methods in an Objective-C protocol can be optional.

    Additionaly in Objective-C you can declare a variable, argument or instance variable to conform to several protocols as well. Example

    NSObject<NSCoding, UITableViewDelegate> *myVariable;
    

    In this case the class must be NSObject or a subclass (only NSProxy and its subclasses would fail), and it must also conform to both NSCoding and UITableViewDelegate protocols.

    In Java or C# this would only be possible by actually declaring said class.