如何使用Swift枚举作为字典键? (符合相当) [英] How can I use a Swift enum as a Dictionary key? (Conforming to Equatable)
问题描述
enum StationSelector:Printable {
case Nearest
case LastShown
case List
case特定(Int)
func toInt() - > Int {
switch self {
case .Nearest:
return -1
case .LastShown:
return -2
case .List:
return -3
case。特定(let stationNum):
return stationNum
}
}
静态func fromInt(value:Int) - > ; StationSelector? {
if value> 0 {
return StationSelector.Specific(value)
}
开关值{
case -1:
return StationSelector.Nearest
case -2:
return StationSelector.LastShown
case -3:
return StationSelector.List
default:
return nil
}
}
var description:String {
get {
switch self {
case .Nearest:
returnNearest Station
case .LastShown:
返回最后显示的站
case .List:
返回站列表
case .Specific(let stationNumber):
返回站#\(stationNumber)
}
}
}
}
d喜欢在字典中使用这些值作为键。声明字典会导致StationSelector不符合Hashable的预期错误。通过简单的哈希函数,符合Hashable很容易:
var hashValue:Int {
get {
return self.toInt()
}
}
然而, Hashable
需要符合 Equable
,我似乎不能在我的枚举上定义equals运算符来满足编译器。 p>
func ==(lhs:StationSelector,rhs:StationSelector) - > Bool {
return lhs.toInt()== rhs.toInt()
}
编译器抱怨这是一行上的两个声明,并希望在 func
之后放置一个;
任何想法?
h2>作为字典键的枚举信息:
从Swift书中:
枚举没有关联值的成员值(如
枚举中所述)默认情况下也是可变的。
但是,您的枚举确实有一个具有关联值的成员值,因此您必须手动添加 Hashable
符合性。
解决方案
您实现的问题是,Swift中的运算符声明必须在全局范围内。
只需移动:
func ==(lhs:StationSelector ,rhs:StationSelector) - > Bool {
return lhs.toInt()== rhs.toInt()
}
检查文档了解更多信息。
I've defined an enum to represent a selection of a "station"; stations are defined by a unique positive integer, so I've created the following enum to allow negative values to represent special selections:
enum StationSelector : Printable {
case Nearest
case LastShown
case List
case Specific(Int)
func toInt() -> Int {
switch self {
case .Nearest:
return -1
case .LastShown:
return -2
case .List:
return -3
case .Specific(let stationNum):
return stationNum
}
}
static func fromInt(value:Int) -> StationSelector? {
if value > 0 {
return StationSelector.Specific(value)
}
switch value {
case -1:
return StationSelector.Nearest
case -2:
return StationSelector.LastShown
case -3:
return StationSelector.List
default:
return nil
}
}
var description: String {
get {
switch self {
case .Nearest:
return "Nearest Station"
case .LastShown:
return "Last Displayed Station"
case .List:
return "Station List"
case .Specific(let stationNumber):
return "Station #\(stationNumber)"
}
}
}
}
I'd like to use these values as keys in a dictionary. Declaring a Dictionary yields the expected error that StationSelector doesn't conform to Hashable. Conforming to Hashable is easy with a simple hash function:
var hashValue: Int {
get {
return self.toInt()
}
}
However, Hashable
requires conformance to Equatable
, and I can't seem to define the equals operator on my enum to satisfy the compiler.
func == (lhs: StationSelector, rhs: StationSelector) -> Bool {
return lhs.toInt() == rhs.toInt()
}
The compiler complains that this is two declarations on a single line and wants to put a ;
after func
, which doesn't make sense, either.
Any thoughts?
Info on Enumerations as dictionary keys:
From the Swift book:
Enumeration member values without associated values (as described in Enumerations) are also hashable by default.
However, your Enumeration does have a member value with an associated value, so Hashable
conformance has to be added manually by you.
Solution
The problem with your implementation, is that operator declarations in Swift must be at a global scope.
Just move:
func == (lhs: StationSelector, rhs: StationSelector) -> Bool {
return lhs.toInt() == rhs.toInt()
}
outside the enum
definition and it will work.
Check the docs for more on that.
这篇关于如何使用Swift枚举作为字典键? (符合相当)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!