Difference between formal and informal protocol

Discussion in 'Mac Programming' started by hrishidev, Nov 4, 2009.

  1. hrishidev macrumors regular

    Dec 3, 2007
    Hi friends ,

    Can anybody tell me the difference between formal and informal protocol
    I read many articles about protocol and category

    what I have understood is

    formal protocol declare methods that ca be implemented by any class
    (i .e. there are not restricted to any class for implementation and can be implemented by any class ) It will be used to capture similarity among classed which are not hierachically related

    Category allows you to add methods to an existing class—even to one to which you do not have the source. they are used to extend the functionality of existing class without sub classing it

    but I cant understand the following statements in apples objective c guide

    In addition to formal protocols, you can also define an informal protocol by grouping the methods in a
    category declaration

    Informal protocols are typically declared as categories of the NSObject class, since that broadly associates the method names with any class that inherits from NSObject

    protocol and category are different concepts , they have different purpose and different syntax for interface declaration then why informal protocol are categories of NSObject class ?

    even if the category is for NSObject class , it should be category not a protocol .....

    I am open for suggestion and criticism but please help me to clarify my confusion
  2. whooleytoo macrumors 604


    Aug 2, 2002
    Cork, Ireland.
    It's a bit counter-intuitive; but as I understand it, a formal protocol is declared with the @protocol directive; whereas informal protocols are actually categories, and declared as categories. So yes, it's odd but they use categories to implement informal protocols.

    Previously, you'd use an informal protocol is some of the methods are optional; but I see now in the docs, they're trying to encourage devs to use formal protocols with optional methods rather than informal protocols.

    The benefit of a formal protocol over informal is the compiler can 'type check' to make sure an object conforms to the protocol that's expected. With informal, it's up to the developer to ensure this.

Share This Page