swift - 这意味着什么:“线程 1:”-[First_App.ProximitySensorViewControllerproximityChanged:]:无法识别的选择器发送到实例 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()
}
}
}
推荐阅读
- java - 是否可以使招摇不显示参考项目api
- python - 在 django rest 框架中验证验证代码
- javascript - 如何从满足某些条件的数组中删除项目。使用角度/打字稿
- flutter - [Flutter]Layout/Build Container 基于其兄弟部件的大小(两个部件都是 Column 的子级)
- android - jitpack - 从服务器收到状态码 521/401/403
- javascript - 除非我打开扩展开发工具,否则上下文菜单不起作用
- php - 将 JMS Serializer 与 PHPSpec 测试用例一起使用导致注释 @JMS\Serializer\Annotation\Type 不存在,或无法自动加载
- javascript - TypeError:messaging.getToken 不是函数
- informatica - Informatica Power Center - 错误:“目标表 [TABLE_NAME] 没有指定键。”
- javascript - 带有 SSR 的 Vue.js 3 中的 Vuex、watch 和生命周期钩子