Darren
Darren

Reputation: 10129

Objective-C coding style - #import or #define at the top of the file

I have a general coding style question for Objective C.

When I have a #define in a file, I put it directly below the #import lines of code and above the @implementation line of code:

#import "MyLibrary.h"

#define myConstant 99

@implementation MyClass

Is this standard style, or is there a standard style of a place to put the defines?

Upvotes: 3

Views: 549

Answers (3)

Gil
Gil

Reputation: 3658

You should always put your #defines after any imports. Otherwise you will pollute the imported files with your #define values and in extreme cases change how they work.

Upvotes: 9

kubi
kubi

Reputation: 49414

I've never seen any standard on this. #defines are almost always at the top of the file (after #imports), but the location doesn't much matter.

Upvotes: 1

dimme
dimme

Reputation: 4424

Objective-C doesn't really use defines to define constants.

However it is possible to do as you do because Objective-C is backwards compatible with C.

This is how I would do it in Objective-C, in the header file:

extern int const MyConstant;

In the implementation file:

int const MyConstant = 99;

Upvotes: 2

Related Questions