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

时间:2018-07-22 18:36:28

标签: swift notificationcenter

首先说

  

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

当我点它时说

  

类型'Notification.Name'(aka'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()

        }
    }
}

6 个答案:

答案 0 :(得分:71)

更改

random_state

forName: Notification.Name.UIApplicationDidEnterBackground

答案 1 :(得分:2)

类型为“ 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)

答案 2 :(得分:2)

Xcode 11.4.1,Swift 5

有完全相同的问题。我的问题是我没有在自定义类中导入UIKit

import UIKit

然后,我能够实现以下目标:

name: UIApplication.didEnterBackgroundNotification

答案 3 :(得分:1)

Xcode 11,快捷键5

UIApplication.didBecomeActiveNotification

答案 4 :(得分:0)

如果UIApplicaiton.didEnterBackgroundNotificaiton不起作用,请尝试.UIApplicationDidEnterBackground

答案 5 :(得分:0)

类似错误onSwap: () { if (OrientationSingleton.left) { OrientationSingleton.left = false; SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeRight]); } else { OrientationSingleton.left = true; SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]); } }

要解决 Swift 5

中的此错误

您应该更改通话方式:

'UIApplicationDidChangeStatusBarOrientation' has been renamed to 'UIApplication.didChangeStatusBarOrientationNotification'

收件人

NotificationCenter.default.addObserver(self, selector: #selector(self.configureActivityIndicatorPosition), name: .UIApplicationDidChangeStatusBarOrientation, object: nil)