当我尝试使用swift 3从iOS中的后台返回时尝试更新应用时应用崩溃

时间:2016-10-26 17:57:25

标签: swift3 ios10

我在viewdidload()

中使用此代码
NotificationCenter.default.addObserver(self, selector: Selector(("appWillEnterForeground:")), name: NSNotification.Name.UIApplicationDidBecomeActive, object: UIApplication.shared)

调用此函数

func appWillEnterForeground(notification: NSNotification!) {
    // update label text here
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)
    NotificationCenter.default.removeObserver(self)
}

但该应用程序在返回前台时崩溃

unrecognized selector sent to instance 0x160db800
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ appWillEnterForeground:]: unrecognized selector sent to instance 0x160db800'

1 个答案:

答案 0 :(得分:0)

appWillEnterForeground:告诉通知中心查找具有该名称的方法,但该方法需要一个参数。您对该方法的实现不带参数,因此不被视为正确的匹配。

尝试Selector(("appWillEnterForeground")