How to create enum with raw type of CGPoint?

后端 未结 3 761
小蘑菇
小蘑菇 2020-12-13 09:59

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

3条回答
  •  一生所求
    2020-12-13 10:35

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

提交回复
热议问题