所以这很奇怪。我刚刚在我的应用程序中创建了一个新的单例,使用的模式与我用于许多其他单例的模式相同。然而,这个在调试器中表现不佳。这是获取单例的代码:
- (id)init
{
self = [super init];
if (self) {
[self loadData];
}
return self;
}
+ (Settings *)sharedInstance
{
static Settings *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
// Do any other initialisation stuff here
});
return sharedInstance;
}
现在,当我尝试从调试器访问对象时,它给了我这个:
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x20).
The process has been returned to the state before expression evaluation.
显然,我不是试图从调试器中获取单例的句柄,而是试图获取一个属性。这给出了一些更有趣的输出:
(lldb) po [Settings sharedInstance].jpegCompressionLevel
error: warning: got name from symbols: Settings
warning: receiver type 'void *' is not 'id' or interface pointer, consider casting it to 'id'
error: no known method '-sharedInstance'; cast the message send to the method's return type
error: 1 errors parsing expression
这里到底发生了什么?代码中的调用似乎很好。调试器不断失败。在相同的上下文中,我可以很好地访问其他单例(使用相同的模式)。
可能值得注意的是,loadData 方法只是从磁盘加载字典,并且类属性的 getter 使用该字典中的值,而不是 ivars。
这是 loadData 的代码:
-(void)loadData
{
// Load data from persistent storage
NSString *path = [self pathToDataFile];
NSMutableDictionary *settingsDict = [NSMutableDictionary dictionaryWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithFile:path]];
_settingsDict = settingsDict;
}