In file CarArray (without any extension) I have the array like that (this is a very simplified version):
NSString *cars[5][3] = {
{@"1A", @"1B", @"1C"},
{@"2A", @"2B", @"2C"},
{@"3A", @"3B", @"3C"},
{@"4A", @"4B", @"4C"},
{@"5A", @"5B", @"5C"}
}
Now I want to read the data from the array in multiple files so I simply use #import "CarArray"
And I use a loop to read the data. Then I get such an error:
duplicate symbol _cars in:
/Users/Adam/Library/Developer/Xcode/DerivedData/Auto_Center-hgjlsqanvyynncgyfzuorxwchqov/Build/Intermediates/Auto Center.build/Debug-iphonesimulator/Auto Center.build/Objects-normal/i386/DetailViewController.o
/Users/Adam/Library/Developer/Xcode/DerivedData/Auto_Center-hgjlsqanvyynncgyfzuorxwchqov/Build/Intermediates/Auto Center.build/Debug-iphonesimulator/Auto Center.build/Objects-normal/i386/ModelListViewController.o
ld: 1 duplicate symbol for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How can I solve that problem?
Now I want to read the data from the array in multiple files so I simply use
#import "CarArray"
This is an incorrect way of accessing array data from multiple places, because it creates multiple definitions in situations when you use the file more than once.
One way of sharing an array would be providing a header with a declaration, and a .m file with an implementation:
CarArray.h:
extern NSString *cars[5][3];
CarArray.m:
#import "CarArray.h"
NSString *cars[5][3] = {
{@"1A", @"1B", @"1C"},
{@"2A", @"2B", @"2C"},
{@"3A", @"3B", @"3C"},
{@"4A", @"4B", @"4C"},
{@"5A", @"5B", @"5C"}
}
Use #import "CarArray.h"
in files from which you wish to use cars
.
Another alternative would be making a class to wrap your global variable, and providing a class method for accessing the array.