Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One really good reason for having a public category, and one that Apple uses often, is for extending a class with functionality that exists in the category's framework, but which doesn't exist in the framework that defines the class. For example, NSString is defined in Foundation.framework, but categories on NSString defining methods for drawing an NSString to the screen are defined in AppKit.framework.</p> <p>Another good usage of categories is for dependency hiding; e g if you really need boost for a part of the class, you can have that in a separate header and implementation file, and a user of the class that needs the boost parts can import that header together with the one originally defining the class, and only that file will take ages to compile. This is more useful in the 64-bit runtime, where a category <em>can</em> add instance variables.</p> <p>A really large implementation over several source files (but a single header) is also a good candidate, as Tom points out :)</p> <p>I'd just like to add to Tom's original answer: Generally, it's better to use a class extension than a class category when declaring private methods. This way, you can implement the extension in the same @implementation block as the public methods, without getting a warning about "missing category implementation". Example:</p> <pre><code>// .h file @interface Foo : NSObject -(void)publicMethod; @end // .m file @interface Foo () // Notice the empty paren; this is how you define // a class extension, which is not the same as a category -(void)somePrivateMethod; @end @implementation Foo #pragma mark Public methods -(void)publicMethod; { ... } #pragma mark Private methods -(void)privateMethod; { ... } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload