我正在尝试使用以下代码为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。我在做什么错了?
答案 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。