Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

swift - How to create enum with raw type of CGPoint?

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 compile

enum MyEnum : CGPoint {
    case Zero
}

with following error

<REPL>:50:15: error: raw type 'CGPoint' is not convertible from any literal
enum MyEnum : CGPoint {
              ^
<REPL>:51:10: error: enum cases require explicit raw values when the raw type is not integer literal convertible
    case Zero
     ^

So how to declare enum with raw type of CGPoint?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...