我想實現的東西給這個效果:返回通用枚舉
enum Foo: Int { case a, b, c, d }
enum Bar: Int { case a, b, c, d }
func generate<T: RawRepresentable>(using index: Int) -> T
{
guard let output = T(rawValue: index) else { preconditionFailure() }
return output
}
但我得到的錯誤:
Playground execution failed: error: GenericEnums.playground:18:24: error: cannot invoke initializer for type 'T' with an argument list of type '(rawValue: Int)'
guard let output = T(rawValue: index) else { preconditionFailure() }
^
GenericEnums.playground:18:24: note: expected an argument list of type '(rawValue: Self.RawValue)'
guard let output = T(rawValue: index) else { preconditionFailure() }
我要去哪裏錯了?
「爲此」不解釋足夠精確你正在嘗試做的。 – matt