Use RawRepresentable with Objective-C enum

Hello,

Can I make objective c enums to conform to RawRepresentable protocol?

Heres an example

typedef enum {
MyEnumType1,
MyEnumType1,
} MyEnum;

extension MyEnum: RawRepresentable {
typealias RawValue = String
}

MyEnum already conforms to RawRepresentable. The following compiles, which it wouldn’t if this weren’t the case.

func f<T>(_ t: T) where T: RawRepresentable{
}

let e: MyEnum = MyEnumType1
f(MyEnumType1)

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

2 Likes