【发布时间】:2016-08-03 17:53:01
【问题描述】:
在 C# 中,您可以编写一个函数,将任何枚举值作为参数,方法是将参数声明为类型 Enum,就像这样...
public enum FirstEnum
{
A,
B,
C
}
public enum SecondEnum
{
D,
E,
F
}
public void StoreValue(Enum key, object value)
{
myDictionary[key] = value; <-- Uses the enum as a key
}
StoreValue(FirstEnum.A, someItem); // Using the first enum type
StoreValue(SecondEnum.D, someItem); // Using the second enum type
StoreValue("Sam", someItem); // Won't compile since 'Sam' is not an enum
你能在 Swift 中做类似的事情吗?我知道您可以将参数 someEnum 声明为字符串并以这种方式使用它,但我正在尝试找出是否可以像在 C# 中那样将其强制为枚举。
【问题讨论】: