Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you declare a constant in Objective-C?

Tags:

objective-c

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?

like image 272
user4951 Avatar asked May 31 '11 13:05

user4951


People also ask

Where do you declare constants?

You use the Const statement to declare a constant and set its value. By declaring a constant, you assign a meaningful name to a value. Once a constant is declared, it cannot be modified or assigned a new value. You declare a constant within a procedure or in the declarations section of a module, class, or structure.

What is used to declare a constant?

Constants are basically variables whose value can't change. In C/C++, the keyword const is used to declare these constant variables. In Java, you use the keyword final .

What does #define do in Objective C?

#define name value , however, is a preprocessor command that replaces all instances of the name with value . For instance, if you #define defTest 5 , all instances of defTest in your code will be replaced with 5 when you compile. Follow this answer to receive notifications.


2 Answers

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; 
like image 184
Dan F Avatar answered Sep 28 '22 03:09

Dan F


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 ; 
like image 35
spacebiker Avatar answered Sep 28 '22 01:09

spacebiker