Objective-C - Syntax - Posing

Posing

Objective-C permits a class to wholly replace another class within a program. The replacing class is said to "pose as" the target class.

Note: Class posing was declared deprecated with Mac OS X v10.5, and is unavailable in the 64-bit runtime.

For the versions still supporting posing, all messages sent to the target class are instead received by the posing class. There are several restrictions:

  • A class may only pose as one of its direct or indirect superclasses.
  • The posing class must not define any new instance variables that are absent from the target class (though it may define or override methods).
  • The target class may not have received any messages prior to the posing.

Posing, similarly with categories, allows global augmentation of existing classes. Posing permits two features absent from categories:

  • A posing class can call overridden methods through super, thus incorporating the implementation of the target class.
  • A posing class can override methods defined in categories.

For example,

@interface CustomNSApplication : NSApplication @end @implementation CustomNSApplication - (void) setMainMenu: (NSMenu*) menu { // do something with menu } @end class_poseAs (, );

This intercepts every invocation of setMainMenu to NSApplication.

Read more about this topic:  Objective-C, Syntax