Objective C protocols usage

前端 未结 3 1324
太阳男子
太阳男子 2020-12-05 08:00

I have a homework question which confused me, really badly. Below is a brief explanation of a question.

Imagine you are developing an application that

3条回答
  •  情歌与酒
    2020-12-05 08:52

    A protocol is the same thing as a Java interface. It just defines which methods the class should support. Here's a page that explains it clearly: http://www.otierney.net/objective-c.html#protocols

    Essentially if you want to make sure a class will have a phoneNumber method (accessor to the phoneNumber property) you would do something like this:

    @protocol ContactProtocol
    -(void) phoneNumber;
    @end
    
    @interface Person: NSObject  {
        ...
    }
    
    @interface Company: NSObject  {
        ...
    }
    

    And then at compile time (or live for xcode 4) it will tell you if you forgot to add the phoneNumber method to the Person or Company classes.

提交回复
热议问题