Inspired from this question. Swift support to create enum with any raw type, so it will be nice to able to create enum with raw type of CGPoint.
But this code won\'t
You can actually have a proper way. This is the code that allows you to have CGPoint as a RawValue of enum:
enum MyPointEnum {
case zero
}
extension MyPointEnum: RawRepresentable {
typealias RawValue = CGPoint
init?(rawValue: CGPoint) {
if rawValue == CGPoint.zero {
self = .zero
} else {
return nil
}
}
var rawValue: CGPoint {
switch self {
case .zero:
return CGPoint.zero
}
}
}
print(MyPointEnum.zero.rawValue) //prints "(0.0, 0.0)\n"