【发布时间】:2015-06-02 13:54:39
【问题描述】:
我不断收到此错误,我不知道为什么。我已经在其他应用程序中实现了这个方法,但由于某种原因它不适用于这个......
我有以下几点:
ViewController.h:
NSInteger HighScore;
ViewController.m:
- (void)viewDidLoad {
...
//load highscores
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighscoreLabel.text = [NSString stringWithFormat:@"%li", (long)HighScore];
}
游戏.m:
#import "ViewController.h"
...
//set/save new highscore
if(Score > HighScore){
[[NSUserDefaults standardUserDefaults] setInteger:Score forKey:@"HighScoreSaved"];
}
并且它不断返回一个失败的构建,并带有一个链接器错误,说“重复符号”。
我很困惑。我什至尝试添加一个全局标头并将其导入到 ViewController 和 Game 中,但仍然出现链接器错误?:
全球.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];
}
游戏.m:
#import "Global.h"
...
//set/save new highscore
if(Score > HighScore){
[[NSUserDefaults standardUserDefaults] setInteger:Score forKey:@"HighScoreSaved"];
}
Xcode 会有问题吗?我已经尝试过典型的“清洁构建”等...... 还是我在做一些非常愚蠢的事情?谢谢。
根据 molbdnilo 的回答更新
虽然这不是我之前实现的方式,但它现在正在使用这个实现:
ViewController.h:
extern NSInteger HighScore;
ViewController.m:
//load highscore
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighscoreLabel.text = [NSString stringWithFormat:@"%li", (long) HighScore];
游戏.h:
NSInteger HighScore; //exactly as declared in ViewController.h
游戏.m:
//if higher score, overwrite
if (Score > HighScore){
[[NSUserDefaults standardUserDefaults] setInteger:Score forKey:@"HighScoreSaved"];
}
【问题讨论】:
-
可能这个
NSInteger HighScore;是你的问题。 -
您介意解释一下吗?