[英]linker command failed with exit code 1 - Xcode
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: ViewController.h:
NSInteger HighScore;
ViewController.m: ViewController.m:
- (void)viewDidLoad {
...
//load highscores
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighscoreLabel.text = [NSString stringWithFormat:@"%li", (long)HighScore];
}
Game.m: 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?: 我什至尝试添加一个全局标头并将其导入到ViewController和Game中,但是仍然收到链接器错误?:
Global.h: Global.h:
#ifndef _Global_h
#define _Global_h
NSInteger HighScore;
#endif
ViewController.m: ViewController.m:
#import "Global.h"
- (void)viewDidLoad {
...
//load highscores
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighscoreLabel.text = [NSString stringWithFormat:@"%li", (long)HighScore];
}
Game.m: 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? Xcode会有问题吗? I've tried the typical "Clean Build" etc... Or am I doing something really dumb? 我已经尝试过典型的“ Clean Build”等……还是我在做一些愚蠢的事情? Thanks. 谢谢。
UPDATE BASED ON molbdnilo's ANSWER 基于molbdnilo的答案的更新
Although it's not how I've implemented it before, it's now working with this implementation: 尽管这不是我以前实现的方式,但现在可以使用此实现:
ViewController.h: ViewController.h:
extern NSInteger HighScore;
ViewController.m: ViewController.m:
//load highscore
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighscoreLabel.text = [NSString stringWithFormat:@"%li", (long) HighScore];
Game.h: Game.h:
NSInteger HighScore; //exactly as declared in ViewController.h
Game.m: Game.m:
//if higher score, overwrite
if (Score > HighScore){
[[NSUserDefaults standardUserDefaults] setInteger:Score forKey:@"HighScoreSaved"];
}
Your HighScore
variable gets one definition each time you include/import the file somewhere. 每当您在某处包含/导入文件时, HighScore
变量都会得到一个定义。
(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”:
extern NSInteger HighScore;
and define it in exactly one source file: 并在一个源文件中定义它:
NSInteger HighScore;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.