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

问题描述 投票:27回答:2

首先,它说

'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()

        }
    }
}
swift notificationcenter
2个回答
66
投票

更改

forName: Notification.Name.UIApplicationDidEnterBackground

forName: UIApplication.didEnterBackgroundNotification

1
投票

类型'NSNotification'的错误在swift4.2中没有成员'UIApplication'

NotificationCenter.default.addObserver(self, selector:#selector(handleNotification), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)

需要相应更改

NotificationCenter.default.addObserver(self, selector:#selector(handleNotification), name: UIApplication.didEnterBackgroundNotification, object: nil)
© www.soinside.com 2019 - 2024. All rights reserved.