Enums 在Swift中如何将类型约束指定为枚举?
我想指定一个类型约束,该类型应为原始值枚举:Enums 在Swift中如何将类型约束指定为枚举?,enums,swift,Enums,Swift,我想指定一个类型约束,该类型应为原始值枚举: enum SomeEnum: Int { case One, Two, Three } class SomeProtocol<E: enum<Int>> { // <- won't compile func doSomething(e: E) { compute(e.toRaw()) } } enum SomeEnum:Int{ 案例一,案例二,案例三 } 类SomeProtoco
enum SomeEnum: Int {
case One, Two, Three
}
class SomeProtocol<E: enum<Int>> { // <- won't compile
func doSomething(e: E) {
compute(e.toRaw())
}
}
enum SomeEnum:Int{
案例一,案例二,案例三
}
类SomeProtocol{/AFAIK,Swift不支持使用枚举指定类型约束
引自
类型约束语法
您可以通过放置单个类或协议来编写类型约束
类型参数名称后的约束,用冒号分隔,如
类型参数列表的一部分。类型约束的基本语法
下面显示了泛型函数的语法(尽管语法相同
对于泛型类型):
严格限制为类或协议,除非手册中没有提到一些隐藏特性。就我测试而言,struct
或enum
都是编译器禁止的
enum Test1 : Int
{
case AAA = 0
}
func test1f<T:Test1>(a: Test1) {} // error: Inheritance from non-protocol, non-class type 'Test1'
struct Test2
{
var aaa:Int = 0
}
func test2f<T:Test2>(a: Test2) {} // error: Inheritance from non-protocol, non-class type 'Test1'
class Test3
{
}
func test3f<T:Test3>(a: Test3) {} // OK
enum Test1:Int
{
案例AAA=0
}
func test1f(a:Test1){}//错误:从非协议、非类类型“Test1”继承
结构测试2
{
变量aaa:Int=0
}
func test2f(a:Test2){}//错误:从非协议、非类类型“Test1”继承
类Test3
{
}
func test3f(a:Test3){}//OK
虽然您可以将枚举放入无约束的泛型类型(
),但不可能为所有枚举或所有结构创建约束。所有约束都基于接口(子类化、协议)。不幸的是,两个随机结构或两个随机枚举之间没有共同点
结构和枚举不能从其他结构/枚举继承,因此枚举的唯一约束必须基于协议
protocol EnumProtocol {
func method()
}
enum TestEnum : Int, EnumProtocol {
case A
case B
func method() {
}
}
enum TestEnum2 : Int, EnumProtocol {
case C
func method() {
}
}
class EnumGeneric <T : EnumProtocol> {
func method(a: T) {
a.method()
}
}
let test = EnumGeneric<TestEnum>()
test.method(TestEnum.A)
但这对于声明为enumtestenum{
enumsomeenum:Int的枚举不起作用{
enum SomeEnum: Int {
case One, Two, Three
}
class SomeClass<E: RawRepresentable where E.RawValue == Int>{
func doSomething(e: E) {
print(e.rawValue)
}
}
class SomeEnumClass : SomeClass<SomeEnum> {
}
案例一,案例二,案例三
}
上课{
func doSomething(e:e){
打印(如原始值)
}
}
类SomeEnumClass:SomeClass{
}
或者直接
class SomeOtherClass{
func doSomething<E: RawRepresentable where E.RawValue == Int>(e: E) {
print(e.rawValue)
}
}
class-SomeOtherClass{
func doSomething(e:e){
打印(如原始值)
}
}
swift3的更新:
enum SomeEnum: Int {
case One, Two, Three
}
class SomeClass<E: RawRepresentable> where E.RawValue == Int {
func doSomething(e: E) {
print(e.rawValue)
}
}
class SomeEnumClass : SomeClass<SomeEnum> {
}
enum SomeEnum:Int{
案例一,案例二,案例三
}
类SomeClass,其中E.RawValue==Int{
func doSomething(e:e){
打印(如原始值)
}
}
类SomeEnumClass:SomeClass{
}
分别
class-SomeOtherClass{
func doSomething(e:e),其中e.RawValue==Int{
打印(如原始值)
}
}
@JMI:你救了我!我差点疯了:我正在使用这个,我想定义一些类似于var dict:[E:MyType]
,但是Xcode 8.2抱怨E不符合Hashable。想法?好吧——我想我现在知道了。在我的例子中,类声明需要从:class MyClass where E:Hashable开始{
虽然这可能会起作用,但它确实感觉像是围绕Swift核心设计原则的终结
enum SomeEnum: Int {
case One, Two, Three
}
class SomeClass<E: RawRepresentable> where E.RawValue == Int {
func doSomething(e: E) {
print(e.rawValue)
}
}
class SomeEnumClass : SomeClass<SomeEnum> {
}
class SomeOtherClass{
func doSomething<E: RawRepresentable>(e: E) where E.RawValue == Int {
print(e.rawValue)
}
}