UserDefaults.didChangeNotification fires unexpectedly

Originator:owenzx
Number:rdar://28928098 Date Originated:2016-10-25
Status:Open Resolved:No
Product:Xcode Product Version:8.1 (8T61a)
Classification:Bug Reproducible:Yes
 
I created a new macOS GUI App. Add a notification observer to UserDefaults.didChangeNotification in ViewController.swift.

I will get one notification when the app starts, one when I click the window that contains the ViewController. 

However, in my opinion, the `UserDefaults.didChangeNotification` shouldn't fire as nothing is changed.

Steps to Reproduce:
1. open the attached project and run.
Or
1. create a new macOS GUI app
2. add below code to ViewController and run

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange), name: UserDefaults.didChangeNotification, object: nil)
    }
    
    override func viewWillDisappear() {
        super.viewWillDisappear()
        
        NotificationCenter.default.removeObserver(self, name: UserDefaults.didChangeNotification, object: nil)
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    func userDefaultsDidChange() {
        print("user defaults did change.")
    }
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!