Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

6.6. Categories

In the previous section, you learned how to override methods in subclasses using the description method from NSObject. The categories design scheme is similar to the subclass method of overriding: it allows developers to add methods to a class that may not be there by default. Rather than creating a subclass and adding the method to it, categories allow a developer to add methods to any existing class without subclassing. This is usually done with the common data structure classes in Objective-C (NSString, NSArray, NSData). It’s often convenient to add a method or two to these classes that will assist with an application you’re making.

6.6.1. Extending classes without subclassing

Reuse is one of the core values of programming. Extending classes is often an effort to minimize the redundancy of code. It compartmentalizes objects and their abilities in a way that allows code reuse instead of copying and pasting code. Sometimes, however, developers want to add to existing classes rather than create their own. Subclassing has an inherent cost because it requires nonstandard class calls throughout an application. If a developer can write a method that attaches to the NSString class, for example, it would lend that piece of code to more common reuse.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint