如何使用NSCoding编码没有rawValue的枚举? [英] How can I encode (with NSCoding) an enum that has no rawValue?
问题描述
我试图使我的类符合 NSCoding
,但是由于其属性之一是枚举而遇到问题,例如:
枚举方向{
例北
例南
}
如果枚举是可编码的,我可以这样做:
class MyClass:NSObject,NSCoding {
var direction:Direction!
需要init(coder aDecoder:NSCoder){
direction = aDecoder.decodeObject(forKey: direction)为!方向
}
函数编码(带有aCoder:NSCoder){
aCoder.encode(direction,forKey: direction)
}
}
但是枚举不是可编码的,因此 encode()
会引发错误
如何使用NSCoder快速编码枚举?建议对枚举的 rawValue
进行编码,然后从对其进行初始化rawValue
在另一端。但是在这种情况下,方向
没有 rawValue
!
它确实具有 hashValue
,这似乎很有希望。我可以毫无问题地对其 hashValue
进行编码,然后在另一端解码回 Int
。但是似乎没有一种方法可以从其 hashValue
初始化一个枚举,因此我无法将其转换回方向
。
如何编码和解码无价值的枚举?
我认为在此处向枚举添加原始值是具有最少代码且最易于维护的解决方案。因此,如果您可以修改枚举,请添加原始值。
现在,假设您无法修改枚举。您仍然可以通过几种方法来做到这一点。
第一个(我认为很丑陋)是添加扩展名
的枚举,并添加如下所示的静态方法:
静态函数方向(来自rawValue:字符串)-> ;方向{
切换rawValue {
例: north:返回.north
例: south:返回.south
默认:fatalError()
}
}
要将方向
转换为一个可编码的值,请使用 String(describing:)
将枚举转换为字符串。要将字符串转换回枚举,只需使用上面的方法。
第二个稍微好一点,但仍然不如添加原始值好。 / p>
您使用字典:
let enumValueDict:[String:Direction ] = [
north:.north, south:.south
]
要将方向
转换为可编码的值,请使用 String(describing:)
将枚举转换为a串。要将字符串转换回枚举,只需访问字典即可。
I'm trying to make my class conform to NSCoding
, but running into problems because one of its properties is an enum, like this:
enum Direction {
case north
case south
}
If enums were codable I could do it like this:
class MyClass: NSObject, NSCoding {
var direction: Direction!
required init(coder aDecoder: NSCoder) {
direction = aDecoder.decodeObject(forKey: "direction") as! Direction
}
func encode(with aCoder: NSCoder) {
aCoder.encode(direction, forKey: "direction")
}
}
but enums aren't codable so encode()
throws an error.
The answers to "How do I encode enum using NSCoder in swift?" suggest encoding the enum's rawValue
and then initializing it from rawValue
on the other end. But in this case, Direction
doesn't have a rawValue
!
It does have a hashValue
, which seems promising. I can encode its hashValue
without a problem, and decode back to an Int
on the other end. But there doesn't seem to be a way to initialize an enum from its hashValue
, so I can't turn it back into a Direction
.
How can I encode and decode a valueless enum?
I think adding a raw value to the enum here is the solution with the least code and is the most maintainable. So if you can modify the enum, add a raw value.
Now let's assume you can't modify the enum. You still can do this in a few ways.
The first one, which I think is quite ugly, is to add an extension
of the enum and add a static method like this:
static func direction(from rawValue: String) -> Direction {
switch rawValue {
case: "north": return .north
case: "south": return .south
default: fatalError()
}
}
To convert Direction
to a codeable value, use String(describing:)
to convert the enum to a string. To convert a string back to an enum, just use the method above.
The second one, slightly better, but still not as good as just adding a raw value.
You use a dictionary:
let enumValueDict: [String: Direction] = [
"north": .north, "south": .south
]
To convert Direction
to a codeable value, use String(describing:)
to convert the enum to a string. To convert a string back to an enum, just access the dictionary.
这篇关于如何使用NSCoding编码没有rawValue的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!