简体   繁体   English

斯威夫特:内嵌的泛型枚举可用于所有类型吗?

[英]Swift: Nested enum in generic that works with all types?

A generic class can have an enum nested inside it: 泛型类中可以嵌套一个枚举:

 class MyClass<T> {

      enum MyEnum { 
      }

      var myEnum: MyEnum
 }

However when using the enum in a protocol the compiler is asking to fill in the type, merely to use the enum. 但是,在协议中使用枚举时,编译器会要求填充类型,仅是为了使用枚举。

 protocol MyProtocol {
      var myEnum: MyClass.MyEnum { get }
      // Error: Reference to generic type 'MyClass' requires arguments in <...>
 }

Is it possible to define a nested enum inside a generic that works with all types? 是否可以在适用于所有类型的泛型中定义嵌套枚举?

Maybe you can use Any or AnyObject: 也许您可以使用Any或AnyObject:

protocol MyProtocol {
    var myEnum: MyClass<Any>.MyEnum{ get }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM