What's the difference between formal and informal protocols in Objective-C?

30,480

Solution 1

From Official Documentation

Formal and Informal Protocols

There are two varieties of protocol, formal and informal:

  • An informal protocol is a category on NSObject, which implicitly makes almost all objects adopters of the protocol. (A category is a language feature that enables you to add methods to a class without subclassing it.) Implementation of the methods in an informal protocol is optional. Before invoking a method, the calling object checks to see whether the target object implements it. Until optional protocol methods were introduced in Objective-C 2.0, informal protocols were essential to the way Foundation and AppKit classes implemented delegation.

  • A formal protocol declares a list of methods that client classes are expected to implement. Formal protocols have their own declaration, adoption, and type-checking syntax. You can designate methods whose implementation is required or optional with the @required and @optional keywords. Subclasses inherit formal protocols adopted by their ancestors. A formal protocol can also adopt other protocols.

Formal protocols are an extension to the Objective-C language.

Solution 2

Informal Protocol : Category (Implementations are Optional)

Formal Protocol : Extension (Implementations are Optional and required)

Solution 3

The Objective-C language provides a way to formally declare a list of methods (including declared properties) as a protocol. Formal protocols are supported by the language and the runtime system. For example, the compiler can check for types based on protocols, and objects can introspect at runtime to report whether or not they conform to a protocol.

Share:
30,480
dontWatchMyProfile
Author by

dontWatchMyProfile

Based in the Czech Republic. Like to swim, like to eat ice cream. The big Cocoa and Cocoa Touch online Training / Session Videos list

Updated on July 09, 2022

Comments

  • dontWatchMyProfile
    dontWatchMyProfile almost 2 years

    What's the difference between formal and informal protocols in Objective-C?

  • Stas
    Stas over 10 years
    so "informal protocol" is just another name for category?
  • byJeevan
    byJeevan about 8 years
    Then what does @required and @optional do ?
  • BlackM
    BlackM almost 8 years
    "informal protocol" is category and "formal protocol" is delegate?
  • selva
    selva over 7 years
    Are Category methods implementations are optional? I don't think so, because I commented a method implementation and immediately I can see error "Method definition for 'xxxmethod' is missing". The xxxmethod is declared in .h file.
  • allenlinli
    allenlinli about 7 years
    Can I have a example of "informal protocol"?
  • lostAtSeaJoshua
    lostAtSeaJoshua almost 7 years
    @allenlinli NSKeyValueCoding is an informal protocol
  • lostAtSeaJoshua
    lostAtSeaJoshua almost 7 years
    @byJeevan @required and @optional are keywords on formal protocols to indicate that methods are required or not required, respectively, to be implemented by the object that is adhereing to it.
  • lostAtSeaJoshua
    lostAtSeaJoshua almost 7 years
    The important thing to note about informal protocols is that they were used heavily before the @optional keyword was introduced to formal protocols in Objective-C 2.0. Before the optional keyword, it was the only way to have an optional protocol method.
  • Prince Kumar Sharma
    Prince Kumar Sharma over 6 years
    Simplest way of describing difference.
  • subin272
    subin272 over 4 years
    @lostAtSeaJoshua Do we have this concept of Informal protocols in Swift ?