Informal Protocol In objective-C?

后端 未结 7 655
旧巷少年郎
旧巷少年郎 2020-11-28 03:55

I was wondering if someone can explain what is informal protocols in Objective C? I try to understand it on apple documentation and some other books but my head is still spi

7条回答
  •  难免孤独
    2020-11-28 04:43

    Based on "Jonathan Sterling" answer, can i say the following code represent informal protocol?

    Apple documentation:

    "When used to declare a protocol, a category interface doesn’t have a corresponding implementation. Instead, classes that implement the protocol declare the methods again in their own interface files and define them along with other methods in their implementation files."

    #import 
    
    @interface Cat1 : NSObject {
    
    
    }
    - (void) simpleMethod;
    
    @end
    
    @implementation Cat1
    
    - (void) simpleMethod
    {
    
        NSLog(@"Simple Method");
    }
    
    @end
    
    
    @interface Cat1 (Cat2) 
    - (void) addingMoreMethods;
    
    @end
    
    
    
    
    @interface MYClass : Cat1
    
    @end
    
    @implementation MYClass
    
    - (void) addingMoreMethods
    {
        NSLog(@"Testing!");
    }
    @end
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    
        MYClass *myclass = [[MYClass alloc] init];
        [myclass addingMoreMethods];
        [myclass release];
        [pool drain];
        return 0;
    }
    

提交回复
热议问题