1

I am really new to Xcode. I don't quite understand exactly what it means when you initialize variables in certain places, but my code works so this isn't the issue... for now. Anyway, here is a my .h file. Everywhere there is a "----" it just means there is code there.

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

NSInteger charactersLocked[3] = {0,0,1};
int charSel = 0;

@interface Tutorial : UIViewController 
{
    -------
}
------
@end

So I need to access the array charactersLocked in a different .m file, lets call it File2 and this one File1. When I try to #import "File1.h" in File2.m, the program crashes and gives me this error for both charactersLocked and charSel:

 duplicate symbol _charactersLocked in:
    /Users/me/Library/Developer/Xcode/DerivedData/SpaceRace-
    apawbkpiogvbvddranqfltyybuqr/Build/Intermediates/SpaceRace.build/Debug- 
    iphoneos/SpaceRace.build/Objects-normal/arm64/Tutorial.o
    /Users/me/Library/Developer/Xcode/DerivedData/SpaceRace
    apawbkpiogvbvddranqfltyybuqr/Build/Intermediates/SpaceRace.build/Debug-
    iphoneos/SpaceRace.build/Objects-normal/arm64/Space.o

I have no idea what this is. I've tried looking for solutions online, but none of them seem to work. I am not importing the .m file by accident. If there is an easier way to just get access to that specific array, please let me know. Sorry if this post was formatted horribly, it is my first time so bear with me. Thank you.

1 Answers1

0

The short answer to your question is located in this answer; you need to declare your variables as extern for the compiler to know that you want to use it in multiple files.


This is very rarely done in Objective-C though, because its heritage as an object-oriented programming language means that global state in programs is most often handled in class methods or singleton classes. Without knowing too much about your program, I am going to guess that you want a singleton class, something like CharacterLocker, which would look like this:

CharacterLocker.h

@interface CharacterLocker : NSObject
{
    NSInteger charactersLocked[3];
}

+ (id) sharedObject;

@property NSInteger* charactersLocked;

@end

CharacterLocker.m

@implementation CharacterLocker

@synthesize charactersLocked;

+ (void) sharedObject
{
    static CharacterLocker *singleton = nil;

    // Required so that multiple calls to sharedObject don't create two
    static dispatch_once_t pred;
    dispatch_once(&pred, ^{ singleton = [[CharacterLocker alloc] init]; });
    return singleton;
}

- (id) init
{
    if ( self = [super init] )
    {
        charactersLocked[0] = 0;
        charactersLocked[1] = 0;
        charactersLocked[2] = 1;
    }
    return self;
}

@end

Then, in your code, you call [CharacterLocker sharedObject].charactersLocked to access the array.

Community
  • 1
  • 1
RC Howe
  • 509
  • 3
  • 16
  • 1
    Thanks man, this really helped me out, but there are still some issues. Im going to try and figure them out myself and if I can't, I will ask again but this time in more detail. Have a good day! – Mike Zakkour Jul 27 '15 at 01:42