What does < > mean / represent in a class interface?

后端 未结 4 1040
盖世英雄少女心
盖世英雄少女心 2021-01-23 04:08

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

@interface GameFinder : NSObject 

        
4条回答
  •  花落未央
    2021-01-23 04:20

    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 
    ...
    
    - (void)methodRequiringBar:(id)arg;
    @end
    
    @interface Foo2 
    ...
    @end
    
    
    id v = [[Foo alloc] init]; //OK
    id 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 
    ...
    @end
    
    @implementation Foo
    - (void)optionalMethod {
    ...
    }
    @end
    

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

提交回复
热议问题