Error and warnings in Xcode when declaring an NSString * array as external external

I declare an NSString * array in the class header file.
PolygonShape.h

NSString* POLYGON_NAMES[] = {@"Invalid Polygon", @"Monogon", ...}; 

Now I use this in PolyginShape.m as follows:

 - (NSString*) name { return (POLYGON_NAMES [self.numberOfSides]); } 

numberOfSides is an iVar that will indicate the index at which the polygon name is stored
So far so good ... it compiled without errors

Then I added PolygonShape.h to my file, which implements the main method (note: they do not have any class definitions and C-Style call functions, not obj-c Style)

 #import "PolygonShape.h" 

Now when I compile, I get a build error ()

 ld: duplicate symbol _POLYGON_NAMES in /Users/../Projects/CS193P/1B/What_A_Tool/build/What_A_Tool.build/Debug/What_A_Tool.build/Objects-normal/i386/PolygonShape.o and /Users/../Projects/CS193P/1B/What_A_Tool/build/What_A_Tool.build/Debug/What_A_Tool.build/Objects-normal/i386/What_A_Tool.o collect2: ld returned 1 exit status 

So, I went through stack overflow and other forums, and basically the advice was to make the global variable extern, and so I did ...

 extern NSString* POLYGON_NAMES[] = {@"Invalid Polygon", @"Monogon" .. }; 

However, I still get a binding error, and also get 2 warnings that say

 warning: 'POLYGON_NAMES' initialized and declared 'extern' 

in the places where I import PolygonShape.h

What am I missing here?

Thanks.

+4
source share
1 answer

In your header file, declare the array as:

 extern const NSString* POLYGON_NAMES[]; 

In the source file, define an array and initialize the contents:

 const NSString* POLYGON_NAMES[] = {@"Invalid Polygon", @"Monogon" }; 
+9
source

All Articles