类型“ UIApplication”没有成员“ didBecomeActiveNotification”

时间:2018-07-28 10:17:30

标签: ios swift uiapplication notificationcenter

我正在尝试使用以下代码为UIApplication.didBecomeActiveNotification添加观察者:

NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { _ in /* some code */ }

但是Xcode说Type 'UIApplication' has no member 'didBecomeActiveNotification'UIApplication类常量,尽管它是officially documented。我在做什么错了?

2 个答案:

答案 0 :(得分:3)

使用Xcode 10时,在 Build Settings 中,如果将 Swift Language Version 设置为Swift 4,则应输入:

NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive, object: nil, queue: nil) { _ in /* some code */ }

如果将其设置为Swift 4.2,请改用它:

NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { _ in /* some code */ }

答案 1 :(得分:1)

您应将通知名称更改为.UIApplicationDidBecomeActive,如下所示:

NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive, object: nil, queue: nil) { _ in /* some code */ }

显然,didBecomeActiveNotification仅适用于iOS 12 SDK。