类型'Notification.Name'(又名'NSNotification.Name')没有成员'UIApplication'

mat*_*lot 37 notificationcenter swift

首先,它说

'UIApplicationDidEnterBackground'已重命名为'UIApplication.didEnterBackgroundNotification'

当我说它时,它说

类型'Notification.Name'(又名'NSNotification.Name')没有成员'UIApplication'

func listenForBackgroundNotification() {
    observer = NotificationCenter.default.addObserver(forName: Notification.Name.UIApplicationDidEnterBackground, object: nil, queue: OperationQueue.main) { [weak self] _ in
        if let weakSelf = self {
            if weakSelf.presentedViewController != nil {
                weakSelf.dismiss(animated: true, completion: nil)
            }
            weakSelf.descriptionTextView.resignFirstResponder()

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

mat*_*att 94

更改

forName: Notification.Name.UIApplicationDidEnterBackground
Run Code Online (Sandbox Code Playgroud)

forName: UIApplication.didEnterBackgroundNotification
Run Code Online (Sandbox Code Playgroud)

  • 它说:“didEnterBackgroundNotification”已重命名为“NSNotification.Name.UIApplicationDidEnterBackground” (3认同)

Dev*_*ami 5

类型“NSNotification”错误在 swift4.2 中没有成员“UIApplication”

NotificationCenter.default.addObserver(self, selector:#selector(handleNotification), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
Run Code Online (Sandbox Code Playgroud)

需要相应改变

NotificationCenter.default.addObserver(self, selector:#selector(handleNotification), name: UIApplication.didEnterBackgroundNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)