Question

As we know, normally we used to declare our class instance variables, properties, method declarations in class header file (.h).

But we can do the same things, in .m file, using blank category.

So my question is: what should be declared in .h file and what should be declared in .m file - and why?

Regards, Mrunal

New Edit:

Hi all,

If you refer to newly added Apple examples over developer.apple.com - they are now declaring their IBOutlets and IBActions in .m file itself and that too with property declaration. But we can achieve the same thing by declaring those references in .h file in class private member section.

Then why are they declaring those in .m file and as properties, any idea?

-Mrunal

Was it helpful?

Solution

But we can do the same things, in .m file, using blank category.

A class continuation.

Normally, you choose to declare something in the header if it is intended to be public -- used by any client. Everything else (your internals) should typically go in the class continuation.

I favor encapsulation -- Here's my approach:

variables

Belongs in the class continuation or @implementation. Exceptions are very, very rare.

properties

Typically belongs in the class continuation in practice. If you want to give subclasses the ability to override these or to make these part of the public interface, then you could declare them in the class declaration (the header file).

method declarations

More in the class continuation than in the class declaration. Again, if it is meant to be used by any client it would belong in the class declaration. Often, you won't even need a declaration in the class continuation (or class declaration) -- the definition alone is adequate if it is private.

OTHER TIPS

Basically, in the header file (.h) you declare your public API, while in the implementation file (.m) you declare your private API.

Visibility in Objective-C

You can also find the answer here

It's mostly up to you.

The .h file is like the description of your class.
It's smart to only put in the .h file what's really important to be visible from the outside of the class, especially if you're working with other developers.

It will help them to understand more easily what methods/properties/variables they can use, rather than having a whole list of things they don't.

Usually you want to use blank category in .m file for declaration of private properties.

// APXCustomButton.m file
@interface APXCustomButton ()

@property (nonatomic, strong) UIColor *stateBackgroundColor;

@end

// Use the property in implementation (the same .m file)
@implementation APXCustomButton

- (void)setStyle:(APXButtonStyle)aStyle
{
    UIColor *theStyleColor = ...;
    self.stateBackgroundColor = theStyleColor;
}

@end

If you try to access property declared in black category outside .m file, you will receive undeclared property compiler error:

- (void)createButton
{
    APXCustomButton *theCustomButton = [[APXCustomButton alloc] init];
    theCustomButton.stateBackgroundColor = [UIColor greenColor]; // undeclared property error
}

In most cases, if you want add new method/properties to an existing class without subclassing, then you want declare category in .h file and implementation of declared methods in .m file

// APXSafeArray.h file
@interface NSArray (APXSafeArray)

- (id)com_APX_objectAtIndex:(NSInteger)anIndex;

@end

// APXSafeArray.m file
@implementation NSArray

- (id)com_APX_objectAtIndex:(NSInteger)anIndex
{
    id theResultObject = nil;
    if ((anIndex >= 0) && (anIndex < [self count]))
    {
        theResultObject = [self objectAtIndex:anIndex];
    }

    return theResultObject;
}

@end

Now you can use "com_APX_objectAtIndex:" method wherever "APXSafeArray.h" is imported.

#import "APXSafeArray.h"

...

@property (nonatomic, strong) APXSafeArray *entities;

- (void)didRequestEntityAtIndex:(NSInteger)anIndex
{
    APXEntity *theREquestedEntity = [self.entities com_APX_objectAtIndex:anIndex];
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top