【发布时间】:2014-09-13 19:04:44
【问题描述】:
我正在尝试对通过 NSNotification 接收的值和枚举值进行简单比较。我有一些有用的东西,但我不敢相信这是做这件事的正确方法。基本上我最终得到的解决方案是将 NSNumber 转换为 Int,并获取枚举值的 rawValue,将其包装在 NSNumber 中,然后获取它的 integerValue。
我尝试的所有其他方法都导致编译器错误,无法在 Uint 8 和 Int 或类似的东西之间进行转换。
observer = NSNotificationCenter.defaultCenter().addObserverForName(AVAudioSessionRouteChangeNotification, object: nil, queue: mainQueue) { notification in
println(AVAudioSessionRouteChangeReason.NewDeviceAvailable.toRaw())
if let reason = notification.userInfo[AVAudioSessionRouteChangeReasonKey!] as? NSNumber {
if (reason.integerValue == NSNumber(unsignedLong:AVAudioSessionRouteChangeReason.NewDeviceAvailable.toRaw()).integerValue) {
self.headphoneJackedIn = true;
} else if (reason.integerValue == NSNumber(unsignedLong:AVAudioSessionRouteChangeReason.OldDeviceUnavailable.toRaw()).integerValue) {
self.headphoneJackedIn = false;
}
self.updateHeadphoneJackLabel()
}
}
【问题讨论】:
-
改用
fromRaw怎么样?