简体   繁体   English

Swift:观察NSUserDefaults时为EXC_BAD_ACCESS

[英]Swift: EXC_BAD_ACCESS when observing NSUserDefaults

I'm writing an iOS app using Swift and want to get notified of NSUserDefault changes. 我正在使用Swift编写iOS应用,并希望收到NSUserDefault更改的通知。 When I register an observer and try to modify the NSUserDefault data afterwards, I always get the following runtime error: EXC_BAD_ACCESS 当我注册一个观察者并尝试之后修改NSUserDefault数据时,总是出现以下运行时错误:EXC_BAD_ACCESS

I've created a little sample app to demonstrate the problem: 我创建了一个小示例应用程序来演示该问题:

import UIKit

class ViewController: UIViewController {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(observeUserDefaults()), name:
                    NSUserDefaultsDidChangeNotification, object: nil)
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: NSUserDefaultsDidChangeNotification, object: nil)
    }

    @IBAction func buttonPressed(sender: UIButton) {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        userDefaults.setObject("test", forKey: "test")
        userDefaults.synchronize()
    }

    func observeUserDefaults() {
        NSLog("observeUserDefaults() called")
    }

}

The method "buttonPressed()" is linked to a UI button on the storyboard. 方法“ buttonPressed()”链接到情节提要上的UI按钮。 As soon as I press the button, the app stops at "userDefaults.setObject("test", forKey: "test")" with error "EXC_BAD_ACCESS (code=1, address=0x0)" The code runs fine if I don't add the observer though. 我一按按钮,应用程序就会在“ userDefaults.setObject(“ test”,forKey:“ test”)“处停止,错误为“ EXC_BAD_ACCESS(code = 1,address = 0x0)”。但是添加观察者。

I've created the same app using Objective-C, which runs without any problems: 我使用Objective-C创建了相同的应用程序,该应用程序运行时没有任何问题:

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(observeUserDefaults) name:NSUserDefaultsDidChangeNotification object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUserDefaultsDidChangeNotification object:nil];
}

- (IBAction)buttonPressed:(UIButton *)sender {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@"test" forKey:@"test"];
    [userDefaults synchronize];
}

- (void)observeUserDefaults {
    NSLog(@"observeUserDefaults() called");
}

@end

Any ideas what is going wrong with the Swift app? 任何想法,Swift应用程序出了什么问题? Thanks for your help! 谢谢你的帮助!

I'm running Xcode 6.1 using the iOS 8.1 SDK. 我正在使用iOS 8.1 SDK运行Xcode 6.1。

Greetings, Felix 菲利克斯的问候

The main error in 主要错误

addObserver(self, selector: Selector(observeUserDefaults()), ...)

is that you call the method instead of providing its selector name: 是您调用方法而不是提供其选择器名称:

addObserver(self, selector: Selector("observeUserDefaults"), ...)

Moreover, the selector should be a method taking a notification object: 此外,选择器应该是采用通知对象的方法:

- (void)observeUserDefaults(notification: NSNotification) {
    NSLog(@"observeUserDefaults() called");
}

Then the registration is 然后注册是

NSNotificationCenter.defaultCenter().addObserver(self,
                   selector: Selector("observeUserDefaults:"),
                   name: NSUserDefaultsDidChangeNotification,
                   object: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM