`UserDefaults.didChangeNotification` fires unexpectedly

In my AppDelegate,


func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Insert code here to initialize your application

    let id = Bundle.main.bundleIdentifier!
    if UserDefaults.standard.persistentDomain(forName: id) == nil {
        UserDefaults.standard.setPersistentDomain(["test":true], forName: id)
    }
}


in my ViewController,


class ViewController: NSViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
         
        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 {
         
        }
    }
    func userDefaultsDidChange() {
        print("user defaults did change.")
    }
}


`UserDefaults.didChangeNotification` fires each time the app running. Why this happened? Nothing is changed, in my opinion.

I removed code in AppDelegate.


    let id = Bundle.main.bundleIdentifier!
    if UserDefaults.standard.persistentDomain(forName: id) == nil {
        UserDefaults.standard.setPersistentDomain(["test":true], forName: id)
    }


Everything is the same. Beside, when I click the Window that contains ViewController when the app runs, `UserDefaults.didChangeNotification` fires another time.

I open a TSI. Apple people suggested me to file a bug on this. Here is the radar https://openradar.appspot.com/radar?id=4949296721952768 rdar://28928098

`UserDefaults.didChangeNotification` fires unexpectedly
 
 
Q