“EXEC_BAD_ACCESS”读取 NSUserDefaults 时出错

发布于 2024-12-09 11:24:34 字数 1090 浏览 3 评论 0原文

当我读取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹梦 2024-12-16 11:24:34

只需删除代码行:[mapStyle release]

stringForKey:将返回一个自动释放的NSString。所以你的代码不负责发布。它在第一次迭代中工作正常,因为第一次释放调用将释放该字符串,但 NSUserDefaults 仍然有一个指向该字符串的指针,但未使用。在第二次迭代中,您获取该指针并尝试在该已释放对象上调用 isEqualToString ,这将导致 BAD_ACCESS

just remove the code-line: [mapStyle release]

the stringForKey: will return an autoreleased NSString. 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 the NSUserDefaults still has a pointer to that String but is not used. In the second iteration you get that pointer and try to call isEqualToString on that dealloc-ed object wich will cause the BAD_ACCESS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文