我以前从未在 Xcode 中注意到这一点,但是当我尝试重用同名 ivar 时出现此错误。我用 2 个 ViewController 创建了一个简单的项目,它们都有 ivar 名称。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
NSString *name;
- (void)viewDidLoad {
[super viewDidLoad];
name = @"me";
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "ViewController2.h"
@interface ViewController2 ()
@end
@implementation ViewController2
NSString *name;
- (void)viewDidLoad {
[super viewDidLoad];
name = @"Me";
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
当我运行这个项目时,我得到了这个错误:
duplicate symbol _name in:
/Users/cta/Library/Developer/Xcode/DerivedData/testDuplicate2-cxeetzeptbwtewfecgmoonzgzeyl/Build/Intermediates/testDuplicate2.build/Debug-iphoneos/testDuplicate2.build/Objects-normal/arm64/ViewController.o
/Users/cta/Library/Developer/Xcode/DerivedData/testDuplicate2-cxeetzeptbwtewfecgmoonzgzeyl/Build/Intermediates/testDuplicate2.build/Debug-iphoneos/testDuplicate2.build/Objects-normal/arm64/ViewController2.o
ld: 1 duplicate symbol for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
最佳答案
在你的实现中 name
不是一个实例变量,它是一个全局变量。事实上,您将它的声明放在 @implementation
block 中并不能使它成为一个实例变量。
如果你想使 name
成为一个实例变量,将其声明为你的类扩展的一部分,如下所示:
@interface ViewController2 () {
NSString *name;
}
@end
请注意,如果您需要 name
为 static
,您的方法会奏效,因为 static
变量在衬里中是“隐藏”的.
关于objective-c - Xcode:为什么我不能在一个项目中有重复的 ivars?出现重复符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665858/