【发布时间】:2018-04-23 13:34:17
【问题描述】:
我正在尝试创建一个 Singleton,以便可以全局使用变量,但出现此错误:
架构 arm64 的未定义符号:
“_OBJC_CLASS_$_GlobalVariables”,引用自: MapViewController.o ld 中的 objc-class-ref:未找到架构 arm64 clang 的符号:错误:链接器命令失败并退出 代码 1(使用 -v 查看调用)
此问题的一些解决方案建议在构建阶段添加第三方库,但我不知道要添加哪个库。这是我的单例类:
.h
@interface GlobalVariables : NSObject
@property BOOL *MAP_SATELLITE_VIEW;
+ (GlobalVariables*)sharedInstance;
@end
.m
#import <Foundation/Foundation.h>
#import "GlobalVariables.h"
@implementation GlobalVariables
@synthesize MAP_SATELLITE_VIEW;
#pragma mark Singleton Methods
+ (GlobalVariables*)sharedInstance {
static GlobalVariables *obj = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
obj = [[self alloc] init];
[obj loadVariables];
});
return obj;
}
- (void)loadVariables {
self.MAP_SATELLITE_VIEW = NO;
}
@end
这就是我尝试从 MapViewController 访问 MAP_SATELLITE_VIEW 变量的方式:
[GlobalVariables sharedInstance].MAP_SATELLITE_VIEW
【问题讨论】:
-
MapViewController.h(或MapViewController.m),他们有#import "GlobalVariables.h"吗?MAP_SATELLITE_VIEW的类别是什么?真的是BOOL(因为它在您的代码中被声明为指针,但在loadVariables中您似乎没有这样对待它。 -
是的,我也有 #import "GlobalVariables.h" 到 .pch 文件
-
你不需要
@synthesize,并且那个ivar不应该是BOOL *,而只是BOOL. I'd also suggest naming the properties in the standard fashion;mapSatelliteViewEnabled`。 -
感谢@bbum 的宝贵意见。
标签: ios objective-c xcode