简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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