“EXEC_BAD_ACCESS”读取 NSUserDefaults 时出错
当我读取 NSUserDefaults (通过 InAppSettingsKit)时发生错误。我不确定是否是我的代码有问题。我已经设置了一个观察者来检查 NSUserDefaults 是否有任何更改:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(setOptions)
name:NSUserDefaultsDidChangeNotification
object:nil];
此调用的方法用于更新 MKMapView 的“地图类型”:
- (void)setOptions
{
// Get the map style
NSString *mapStyle = [[NSUserDefaults standardUserDefaults] stringForKey:kMapType];
// Update map style
if ([mapStyle isEqualToString:@"Hybrid"])
{
map.mapType = MKMapTypeHybrid;
}
else if ([mapStyle isEqualToString:@"Map"])
{
map.mapType = MKMapTypeStandard;
}
else if ([mapStyle isEqualToString:@"Satellite"])
{
map.mapType = MKMapTypeSatellite;
}
[mapStyle release];
}
该应用程序的设置是让您按下按钮并初始化 InAppSettingsKit ,在此我更改要显示的地图类型的设置并返回到我的应用程序的主屏幕。此时地图似乎已正确更新并且没有任何问题。当我尝试重新启动 InAppSettingsKit 以再次更改地图类型时,会出现此问题。
有谁知道是否是我的代码有问题,如果是,我该如何修复它?
I'm having an error occur when I read the NSUserDefaults (via InAppSettingsKit). I'm not sure if it's my code that's the issue though. I have set up an observer to check if there are any changes to the NSUserDefaults:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(setOptions)
name:NSUserDefaultsDidChangeNotification
object:nil];
The method this calls is used to update the 'map type' of an MKMapView:
- (void)setOptions
{
// Get the map style
NSString *mapStyle = [[NSUserDefaults standardUserDefaults] stringForKey:kMapType];
// Update map style
if ([mapStyle isEqualToString:@"Hybrid"])
{
map.mapType = MKMapTypeHybrid;
}
else if ([mapStyle isEqualToString:@"Map"])
{
map.mapType = MKMapTypeStandard;
}
else if ([mapStyle isEqualToString:@"Satellite"])
{
map.mapType = MKMapTypeSatellite;
}
[mapStyle release];
}
The app is set up such that you press a button and the InAppSettingsKit is initialised, within this I change the setting for the map type to be displayed and go back to the main screen in my app. At this point the map seems to update correctly and there are no issues. The issue occurs when I try to re-launch the InAppSettingsKit to change the map type again.
Does anyone know if it's my code that's the issue, if so how can I go about fixing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需删除代码行:
[mapStyle release]
,stringForKey:
将返回一个自动释放的NSString
。所以你的代码不负责发布。它在第一次迭代中工作正常,因为第一次释放调用将释放该字符串,但 NSUserDefaults 仍然有一个指向该字符串的指针,但未使用。在第二次迭代中,您获取该指针并尝试在该已释放对象上调用isEqualToString
,这将导致 BAD_ACCESSjust remove the code-line:
[mapStyle release]
the
stringForKey:
will return an autoreleasedNSString
. So your code is not responsible for releasing. It works fine in the first iteration because the first release call will dealloc that string but theNSUserDefaults
still has a pointer to that String but is not used. In the second iteration you get that pointer and try to callisEqualToString
on that dealloc-ed object wich will cause the BAD_ACCESS