Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C : #define vs extern const

I know this question has been asked before, but I can't seem to find information about it in Apple's documentation; maybe some of you guys did.

A lot of Objective-C code has cross-file constants in a .h file, using #define. Others use the approach of a .m with constants and extern them in the .h file.

I understand the difference, both pros and cons, but does Apple state which one to use in iOS development?

like image 380
DCMaxxx Avatar asked Dec 05 '13 10:12

DCMaxxx


People also ask

What is Objective-C used for?

Objective-C is general-purpose language that is developed on top of C Programming language by adding features of Small Talk programming language making it an object-oriented language. It is primarily used in developing iOS and Mac OS X operating systems as well as its applications.

Is Objective-C harder than C++?

In my opinion, probably the biggest difference is the syntax. You can achieve essentially the same things in either language, but in my opinion the C++ syntax is simpler while some of Objective-C's features make certain tasks (such as GUI design) easier thanks to dynamic dispatch.

Is Objective-C better than Swift?

Objective-C has a superior runtime compared to Swift. It's probably going to be several years before Swift can catch up. If you're using powerful SDKs, Objective-C is also your best option here as well. I'd still recommend that new developers start off learning Swift.

Is Objective-C still used?

Most of the core iOS and MacOs software is still written in Objective-C, though Apple is pushing for new updates to be written in Swift.


2 Answers

Apple's recommendation is extern:

Define constants for strings used for such purposes as notification names and dictionary keys. By using string constants, you are ensuring that the compiler verifies the proper value is specified (that is, it performs spell checking).

Admittedly they are inconsistent about this sometimes.

like image 96
ilya n. Avatar answered Sep 28 '22 05:09

ilya n.


A #define defines a macro which is replaced before compilation starts where as extern *** *const merely modifies a variable so that the compiler will flag an error if you try to change it. There are some cases in that you would use a #define because you can't use a extern *** *const. In theory a extern *** *const will take up memory and requires a reference to memory but this is insignificant as it maybe optimized away from the compiler.

extern *** *consts are a lot more compiler and debug friendlier then #defines this can be the deciding point when you decide which one to use.

Some see that pre-processor directives like #define are frowned upon which would suggest you should be using extern *** *const over #define

But whilst the pre-processor is frowned open some say it is more secure then a variable as it can't be changed at runtime whereas a variable can.

Both have there advantages and disadvantages and I don't think (I can't find anything myself) that Apple recommends one over the other. My personal opinion is to use a mix of them both using a pre-processor directive #define over a extern *** *const where it would seem more beneficial, this is what I do.

like image 22
Popeye Avatar answered Sep 28 '22 07:09

Popeye