I'm familiar with Objective-C class extensions in class main implementation file, but curious for what the new Xcode 4.4 "An Objective-C class extension header" file template is intended for?
Objective C files In objective C, files normally use the extension . h for interface files and the extension . m for implementation files.
A category allows you to add methods to an existing class—even to one for which you do not have the source. Categories are a powerful feature that allows you to extend the functionality of existing classes without subclassing.
Class extensions must be implemented in the main @implementation
block, but the declaration can be anywhere.
Extensions are used to add something to the class interface that you do not want to be public, and therefore cannot put in the public class declaration header.
Declaring the extension in the same file of the implementation, which you are familiar with, is used when the extension is only used by the class implementation itself.
Declaring the extension in a separate header, which is what the template is for, is useful when you develop a framework. The extension header will not be part of the set of public headers, but will be used internally by more than one implementation file of the framework.
You can think of it as private to framework instead of private to class.
Another reason is that a Class Extension Header file is useful in the case you are using Unit Test to test your class, putting class extension in a separate header file allow you to import the the header containing private methods into the implementation of the class that needs those private methods and into the unit test file that you use to test the class ...
This is a very good addition(the opportunity to test private methods in the unit test) to Objective-C in my opinion, it is a thing that you can't do at the moment with JUnit in Java .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With