【发布时间】:2016-07-01 16:39:44
【问题描述】:
我们在 ObjC 中有一些执行 JSON 序列化/反序列化的现有代码。在这些数据对象的 .h 文件之一中,我们有类似的内容:
DataObject.h
@class DataObject
typedef NS_ENUM(NSInteger, FriendStatus)
{
FriendStatusMyself = -1,
FriendStatusNotFriends = 0,
FriendStatusFriends = 1,
FriendStatusPendingIncoming = 2,
FriendStatusPendingOutgoing = 3
};
@interface DataObject : MTLModel <MTLJSONSerializing>
@property (nonatomic, strong) NSNumber *friendStatus;
// more stuff...
@end
现在这对 JSON 序列化非常有效,并且一切正常。嗯,有点。
在我的 swift 类中,我想使用 DataObject,但引用 friendStatus 作为
FriendStatus 枚举,所以我最终经常使用 .rawValue。例如
RandomClass.swift
if (dataObject.friendStatus == FriendStatus.PendingIncoming.rawValue) {
// do something
}
这行得通,可以说这是相对较小的,但到处使用.rawValue 似乎很糟糕(tm)。有没有办法进行转换,所以 DataObject.friendStatus 是真正的 FriendStatus 枚举,我可以停止在 swift 中使用 .rawValue?
不幸的是,我可以对我的模型(DataObject)进行的更改有限,因为它是现有代码。
【问题讨论】:
标签: objective-c swift enums