这是什么意思:"Thread 1: "-[First_App.ProximitySensorViewController proximityChanged:]: 无法识别的选择器发送到实例 0x14d707940""
What means this: "Thread 1: "-[First_App.ProximitySensorViewController proximityChanged:]: unrecognized selector sent to instance 0x14d707940""
嘿,我正在尝试使用接近传感器来增加一个值,但是当我启动该功能时,它崩溃了。这是我的代码,有人可以帮助我吗?
func proximityChanged(notification: NSNotification) {
let device = notification.object as? UIDevice
if device?.proximityState == true {
print("\(device) detected!")
count += 1
updateCountLabel()
} else {
print("Error")
}
}
func activateProximitySensor() {
let device = UIDevice.current
device.isProximityMonitoringEnabled = true
if device.isProximityMonitoringEnabled {
NotificationCenter.default.addObserver(self, selector: Selector(("proximityChanged:")), name: NSNotification.Name(rawValue: "UIDeviceProximityStateDidChangeNotification"), object: device)
}
}
我认为您可能对 swift 使用了旧语法。我尝试使用以下代码使用接近度并且没有崩溃。
func activateProximitySensor() {
let device = UIDevice.current
device.isProximityMonitoringEnabled = true
if device.isProximityMonitoringEnabled {
NotificationCenter.default.addObserver(self, selector: #selector(proximityChanged(notification:)), name: UIDevice.proximityStateDidChangeNotification, object: device)
}
}
@objc func proximityChanged(notification: NSNotification) {
if let device = notification.object as? UIDevice {
if device.proximityState {
print("\(device) detected!")
count += 1
updateCountLabel()
}
}
}
嘿,我正在尝试使用接近传感器来增加一个值,但是当我启动该功能时,它崩溃了。这是我的代码,有人可以帮助我吗?
func proximityChanged(notification: NSNotification) {
let device = notification.object as? UIDevice
if device?.proximityState == true {
print("\(device) detected!")
count += 1
updateCountLabel()
} else {
print("Error")
}
}
func activateProximitySensor() {
let device = UIDevice.current
device.isProximityMonitoringEnabled = true
if device.isProximityMonitoringEnabled {
NotificationCenter.default.addObserver(self, selector: Selector(("proximityChanged:")), name: NSNotification.Name(rawValue: "UIDeviceProximityStateDidChangeNotification"), object: device)
}
}
我认为您可能对 swift 使用了旧语法。我尝试使用以下代码使用接近度并且没有崩溃。
func activateProximitySensor() {
let device = UIDevice.current
device.isProximityMonitoringEnabled = true
if device.isProximityMonitoringEnabled {
NotificationCenter.default.addObserver(self, selector: #selector(proximityChanged(notification:)), name: UIDevice.proximityStateDidChangeNotification, object: device)
}
}
@objc func proximityChanged(notification: NSNotification) {
if let device = notification.object as? UIDevice {
if device.proximityState {
print("\(device) detected!")
count += 1
updateCountLabel()
}
}
}