Where do you declare a constant in Objective-C?

Objective C

Objective C Problem Overview


I declared a constant in a header file const double EARTH_RADIUS=6353; that is imported to various other headers and I got a linker error.

Ld /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator/BadgerNew.app/BadgerNew normal i386
    cd /Users/Teguh/Dropbox/badgers/BadgerNew
    setenv MACOSX_DEPLOYMENT_TARGET 10.6
    setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/llvm-g++-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk -L/Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator -F/Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator -filelist /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Intermediates/BadgerNew.build/Debug-iphonesimulator/BadgerNew.build/Objects-normal/i386/BadgerNew.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -framework CoreLocation -framework UIKit -framework Foundation -framework CoreGraphics -framework CoreData -o /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator/BadgerNew.app/BadgerNew

ld: duplicate symbol _EARTH_RADIUS in /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Intermediates/BadgerNew.build/Debug-iphonesimulator/BadgerNew.build/Objects-normal/i386/NearbyIsiKota.o and /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Intermediates/BadgerNew.build/Debug-iphonesimulator/BadgerNew.build/Objects-normal/i386/FrontPageofBadger.o for architecture i386
collect2: ld returned 1 exit status

Basically I want the constant to be available for all classes on my project. Where should I declare it?

Objective C Solutions


Solution 1 - Objective C

You can declare in the header, define it in a code file. Simply declare it as

extern const double EARTH_RADIUS;

then in a .m file somewhere (usually the .m for the .h you declared it in)

const double EARTH_RADIUS = 6353;

Solution 2 - Objective C

There are two ways to accomplish this:

1st option- As previous replies pointed, in the .h file:

myfile.h
extern const int MY_CONSTANT_VARIABLE;

and in myfile.m define them

myfile.m    
const int MY_CONSTANT_VARIABLE = 5;

2nd option- My favourite:

myfile.h
static const int MY_CONSTANT_VARIABLE = 5 ;

Solution 3 - Objective C

Declare it in a source file and have external linkage to it( using extern keyword ) so as to use in all other source files.

Solution 4 - Objective C

Best practice would be to declare it in your .h and .m files. See https://stackoverflow.com/questions/538996/constants-in-objective-c for a very detailed set of answers regarding this same question.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser4951View Question on Stackoverflow
Solution 1 - Objective CDan FView Answer on Stackoverflow
Solution 2 - Objective CspacebikerView Answer on Stackoverflow
Solution 3 - Objective CMaheshView Answer on Stackoverflow
Solution 4 - Objective CKyle CleggView Answer on Stackoverflow