【发布时间】:2018-01-25 15:20:54
【问题描述】:
我想将 userInfo 数据从 NSNotification 转换为枚举以在 switch 语句中使用。通知来自预编译的 c++ 框架,其中一些头文件定义了枚举。
-(void)updateOTAStatus:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
otaUpgradeStatus status = (otaUpgradeStatus)userInfo[@"status"];
//...
}
枚举定义:
typedef NS_ENUM(NSUInteger, otaUpgradeStatus) {
none = 0,
started = 1,
inProgress = 2,
completed = 3,
failed = 4,
failedLowBattery = 5,
cancelled = 6,
timedout = 7,
forced = 8
};
调试时我得到了
Printing description of status:
(otaUpgradeStatus) status = 6178538944
当我在 Swift 中执行相同操作时,switch 语句失败:
let status = notification.userInfo?["status"] as? otaUpgradeStatus
我得到了正确的状态,switch语句按预期工作。
这里出了什么问题?
【问题讨论】:
-
我的猜测是状态在字典中存储为
NSNumber。试试NSNumber *num = userInfo[@"status"]; NSInteger status = num.integerValue; -
然后我得到 -[_SwiftValue integerValue]: unrecognized selector sent to instance 0x174452420,这可能是因为通知是从 swift 触发的,然后转到 c++ 库,然后转到目标 c 模块。我可以在堆栈跟踪中看到这一点。
-
您是否创建通知?在这种情况下,将状态作为
NSNumber添加到信息字典中应该会有所帮助。 -
类似
userInfo: ["status": NSNumber(value: otaUpgradeStatus.failed.rawValue)] -
天哪,谢谢!有效!
标签: c++ objective-c swift enums