【问题标题】:Type 'Notification.Name' (aka 'NSNotification.Name') has no member 'UIApplication'类型“Notification.Name”(又名“NSNotification.Name”)没有成员“UIApplication”
【发布时间】:2018-07-22 18:36:27
【问题描述】:

首先它说

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

当我点它时,它说

类型“Notification.Name”(又名“NSNotification.Name”)没有成员 'UI应用'

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


【解决方案1】:

改变

forName: Notification.Name.UIApplicationDidEnterBackground

forName: UIApplication.didEnterBackgroundNotification

【讨论】:

  • 它说:“didEnterBackgroundNotification”已重命名为“NSNotification.Name.UIApplicationDidEnterBackground”
【解决方案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)

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      Xcode 11,快速 5

      UIApplication.didBecomeActiveNotification
      

      【讨论】:

      • 你忘了import UIKit
      【解决方案5】:

      Xcode 11.4.1、Swift 5

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

      import UIKit
      

      然后我能够实现以下内容:

      name: UIApplication.didEnterBackgroundNotification
      

      【讨论】:

        【解决方案6】:

        类似错误'UIApplicationDidChangeStatusBarOrientation' has been renamed to 'UIApplication.didChangeStatusBarOrientationNotification'

        Swift 5

        中解决此错误

        你应该改变你打电话的方式:

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

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

        【讨论】:

          猜你喜欢
          • 2020-08-11
          • 2019-07-08
          • 2019-02-18
          • 2019-02-18
          • 2019-09-17
          • 1970-01-01
          • 2019-04-26
          • 2019-04-02
          • 1970-01-01
          相关资源
          最近更新 更多