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
I really like Bryan Chen's solution but I came with one alternative. It doesn't really uses enums:
extension CGPoint : RawRepresentable, Equatable {
typealias RawType = (CGFloat, CGFloat)
static let Zero = CGPointZero
static let One = CGPoint(x: 1.0, y: 1.0)
static let MagicPoint = CGPoint(x: 42.0, y: 0.0)
static func fromRaw(raw: RawType) -> CGPoint? {
return CGPoint(x: raw.0, y: raw.1)
}
func toRaw() -> RawType {
return (x, y)
}
}
Now we can do all that "raw" operations:
var p = CGPoint.fromRaw((10, 20)) //from tuple
We can also use a switch
:
switch (p) {
case CGPoint.Zero:
println("p is (0, 0)")
case CGPoint.One:
println("p is (1, 1)")
case CGPoint.MagicPoint:
println("p is (42, 0)")
case CGPoint(x: 0, y: 10):
println("p is (0, 10)")
default:
println("Something else")
}
However, you need the default
case and you cannot use the short .Zero
names.
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"
There are two errors in given code.
First one is
error: raw type 'CGPoint' is not convertible from any literal
enum MyEnum : CGPoint {
So we need to make CGPoint convertible from literal
One way to solve it is to extend CGPoint to make it convertible from String literal by conform StringLiteralConvertible
extension CGPoint : StringLiteralConvertible {
static func convertFromStringLiteral(value: String) -> CGPoint {
return NSPointFromString(value) // CGPointFromString on iOS
}
static func convertFromExtendedGraphemeClusterLiteral(value: String) -> CGPoint {
return NSPointFromString(value) // CGPointFromString on iOS
}
}
we can create CGPoint from string literal now!
var p : CGPoint = "2,3"
println(p) // print (2.0,3.0)
The second error is
error: enum cases require explicit raw values when the raw type is not integer literal convertible
case Zero
^
which is easy to fix now, just assign some string literal to it
enum MyEnum : CGPoint {
case Zero = "0, 0"
case One = "1, 1"
case MagicPoint = "0, 42"
}
println(MyEnum.Zero.toRaw()) // (0.0,0.0)
println(MyEnum.One.toRaw()) // (1.0,1.0)
println(MyEnum.MagicPoint.toRaw()) // (0.0,42.0)
and now you have enum with CGPoint raw type
to use it
if let p = MyEnum.fromRaw(CGPoint(x:0, y:42)) {
switch (p) {
case .Zero:
println("p is (0, 0)")
break
case .One:
println("p is (1, 1)")
break
case .MagicPoint:
println("p is magic point")
break
}
}
// print "p is magic point"
It will be nicer to create CGPoint from tuple, however, looks like it is not possible.
From the grammar
literal → integer-literal floating-point-literal string-literal
there are only three types of literal, so string-literal is the only option here (unless you want 1.2
to be (1, 2)
)