0

I keep getting this error and I don't know why. I've implemented this method in other applications but for some reason it's not working for this one...

I have the following:

ViewController.h:

    NSInteger HighScore;

ViewController.m:

 - (void)viewDidLoad {
      ...
      //load highscores
      HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
      HighscoreLabel.text = [NSString stringWithFormat:@"%li", (long)HighScore];
 }

Game.m:

 #import "ViewController.h"
 ...
 //set/save new highscore
 if(Score > HighScore){
    [[NSUserDefaults standardUserDefaults] setInteger:Score forKey:@"HighScoreSaved"];
 }

And it keeps returning a fail build with a linker error saying "duplicate symbol".

I'm so confused. I even tried adding a global header and importing it into both ViewController and Game, but still I get the linker error?:

Global.h:

 #ifndef _Global_h
 #define _Global_h

 NSInteger HighScore;

 #endif

ViewController.m:

 #import "Global.h"

 - (void)viewDidLoad {
      ...
      //load highscores
      HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
      HighscoreLabel.text = [NSString stringWithFormat:@"%li", (long)HighScore];
 }

Game.m:

 #import "Global.h"
 ...
 //set/save new highscore
 if(Score > HighScore){
    [[NSUserDefaults standardUserDefaults] setInteger:Score forKey:@"HighScoreSaved"];
 }

Would there be an issue with Xcode? I've tried the typical "Clean Build" etc... Or am I doing something really dumb? Thanks.

UPDATE BASED ON molbdnilo's ANSWER

Although it's not how I've implemented it before, it's now working with this implementation:

ViewController.h:

 extern NSInteger HighScore;

ViewController.m:

 //load highscore
 HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
 HighscoreLabel.text = [NSString stringWithFormat:@"%li", (long) HighScore];

Game.h:

 NSInteger HighScore; //exactly as declared in ViewController.h

Game.m:

 //if higher score, overwrite
 if (Score > HighScore){
     [[NSUserDefaults standardUserDefaults] setInteger:Score forKey:@"HighScoreSaved"];
 } 
2
  • May be this NSInteger HighScore; is your problem. Commented Jun 2, 2015 at 13:59
  • Would you mind explaining how so? Commented Jun 2, 2015 at 14:01

1 Answer 1

1

Your HighScore variable gets one definition each time you include/import the file somewhere.
(For the gory details, look up the "translation unit" concept.)

If you really, really want to use a global variable, you need to declare it "extern" in a header:

extern NSInteger HighScore;

and define it in exactly one source file:

NSInteger HighScore;
Sign up to request clarification or add additional context in comments.

1 Comment

Well this works so thanks for posting. It's just not how I've implemented it before which has just plain confused me, because it works in my last app, and in my head there's no reason it shouldn't work. There must be a setting in xCode that's conflicting it this time. I will update my code to show everyone how I implemented it based on your answer. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.