views:

151

answers:

4

I am sure I have read this somewhere, Can anyone tell me what the < > represent in the following interface?

@interface GameFinder : NSObject <NSNetServiceBrowserDelegate>
@end

is NSObject adopting <NSNetServiceBrowserDelegate> ?

EDIT

One thing that is confusing me ...

in the example I have.The interface shows NSNetServiceBrowserDelegate

@interface ITunesFinder : NSObject <NSNetServiceBrowserDelegate>
@end

but the implementation shows netServiceBrowser, are these one in the same?

@implementation ITunesFinder
-(void) netServiceBrowser: (NSNetServiceBrowser *) browser
           didFindService: (NSNetService *) service
               moreComing: (BOOL) moreComing {

gary

+5  A: 

< > represent a protocol (or list of protocols) to which a class conforms. An Objective-C protocol is like an interface in Java: it's a list of methods that the conforming class must implement.

mipadi
+1 For a short and sweet answer. Just one small thing; it is a list of methods that the conforming class _should_ implement. There is no _must_.
PeyloW
True, but gcc throws a warning if you don't implement all the methods.
mipadi
+12  A: 

The angle brackets denote Protocols that this class meets. There are details on Protocols int the Objective-C Wikipedia article that may help clear up some things for you. Protocols contain both required and optional routines that your class could supply. In the latter case if the routine is not implemented by your class a default implementation/behavior is used instead.

fbrereto
+2  A: 

The angle brackets in an interface declaration denote the list of Objective-C protocols that the interface implements. In this case, that GameFinder conforms to the NSNetServiceBrowserDelegate protocol. The Objective-C Language Reference has a full section on protocols (and is a reference you should keep handy in general while learning Objective-C). Basically, a Protocol is an interface that describes the methods a class must implement to conform to that protocol. Classe interfaces may declare, using the angle bracket notation, that they conform to (implement) a protocol. The compiler will check protocol conformance if you provide protocol information in type declarations:

@interface Foo <Bar>
...

- (void)methodRequiringBar:(id<Bar>)arg;
@end

@interface Foo2 <Baz>
...
@end


id<Bar> v = [[Foo alloc] init]; //OK
id<Baz> v = [[Foo alloc] init]; //warning

[v methodRequiringBar:[[Foo2 alloc] init]]; //warning

The compiler will also warn you if a class interface declares conformance to a protocol but not all of the required methods in that protocol are implemented by the class' implementation:

@protocol Bar
@required
- (void)requiredMethod;
@optional
- (void)optionalMethod;
@end

@interface Foo <Bar>
...
@end

@implementation Foo
- (void)optionalMethod {
...
}
@end

will give a warning that the Bar protocol is not fully implemented.

Barry Wark
A: 

NSNetServiceBrowser is a class. NSNetServiceBrowserDelegate is a protocol specifying what methods an NSNetServiceBrowser's delegate must implement.

Chuck
Thanks Chuck, so netServiceBrowser is a method of NSNetServiceBrowser?
fuzzygoat
AFAIK, there is no such method as "netServiceBrowser". If you're talking about `netServiceBrowser:didFindService:moreComing:`, then it's a method that an NSNetServiceBrowser's delegate can implement.
Chuck
Excellent, thank you.
fuzzygoat