Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An Objective-C class extension header - for what?

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?

like image 823
user1591976 Avatar asked Aug 11 '12 09:08

user1591976


People also ask

What is the extension of Objective-C?

Objective C files In objective C, files normally use the extension . h for interface files and the extension . m for implementation files.

What is category and extension in ios?

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.


2 Answers

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.

like image 50
Analog File Avatar answered Sep 19 '22 08:09

Analog File


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 .

like image 38
aleroot Avatar answered Sep 17 '22 08:09

aleroot