1

Possible Duplicate:
Xcode duplicate symbol error

I have a Constant.h file that stores my constants strings. But when I try to add Constant.h file to two different classes (using #import "Constant.h") I got error with duplicate symbol. How can I use this file in different classes without duplicate symbol error?

Community
  • 1
  • 1
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

2 Answers2

4

You’re declaring the constant wrong. The right approach for NSString constants is:

extern NSString *const ConstantName; // in Constant.h
NSString *const ConstantName = @"ConstantName"; // in Constant.m

See also this question and related ones.

Community
  • 1
  • 1
zoul
  • 102,279
  • 44
  • 260
  • 354
  • Yes, thanks I have used the same variant for a long time and this really works. One problem with that when I try to add new extern variable the other classes can't see this variable in first time and I need to make one magic action to break this enchantment. Close project, delete derived data in organizer delete project.xcworkspace file and xcuserdata folder in xcodeproj folder. but in any case it works. – Matrosov Oleksandr Nov 15 '12 at 11:27
  • I meant. You are right. Enchantment is not connected to this question :) Thanks again! – Matrosov Oleksandr Nov 15 '12 at 11:32
-2

Try using the below code to use constants file in 2 different classes

     @class Constant; 

use this before @implementation in .m file of the viewcontroller

Jasmeet Singh
  • 564
  • 3
  • 9
  • This has nothing to do with the issue. The constant in the .h file needs to be marked as `extern` to avoid this problem. – rmaddy Nov 14 '12 at 17:49