小编Han*_*ans的帖子

在 Objective c 中,从 NSNotification 中的 Userinfo 转换 NSEnum 失败

我想将 userInfo 数据从 NSNotification 转换为枚举以在 switch 语句中使用。该通知来自预编译的 C++ 框架,其中包含一些定义 te 枚举的头文件。

-(void)updateOTAStatus:(NSNotification *)notification {
  NSDictionary *userInfo = notification.userInfo;
  otaUpgradeStatus status = (otaUpgradeStatus)userInfo[@"status"];
  //...
}
Run Code Online (Sandbox Code Playgroud)

枚举定义:

typedef NS_ENUM(NSUInteger, otaUpgradeStatus) {
  none = 0,
  started = 1,
  inProgress = 2,
  completed = 3,
  failed = 4,
  failedLowBattery = 5,
  cancelled = 6,
  timedout = 7,
  forced = 8
};
Run Code Online (Sandbox Code Playgroud)

调试时我得到

Printing description of status:
(otaUpgradeStatus) status = 6178538944
Run Code Online (Sandbox Code Playgroud)

当我在 Swift 中执行相同操作时, switch 语句失败:

let status = notification.userInfo?["status"] as? otaUpgradeStatus
Run Code Online (Sandbox Code Playgroud)

我得到了正确的状态,并且 switch 语句按预期工作。 …

c++ enums objective-c swift

1
推荐指数
1
解决办法
409
查看次数

标签 统计

c++ ×1

enums ×1

objective-c ×1

swift ×1