2

In iOS 8.0 I had this enum:

enum Direction:CGPoint {
    case Top = "{0, -1}"
    case Right = "{1, 0}"
    case Down = "{0, 1}"
    case Left = "{-1, 0}"

    case TopLeft = "{-1, -1}"
    case TopRight = "{1, -1}"
    case DownLeft = "{-1, 1}"
    case DownRight = "{1, 1}"

    case None = "{0, 0}"
}

extension CGPoint: StringLiteralConvertible {

    public static func convertFromStringLiteral(value: StringLiteralType) -> CGPoint {
        return CGPointFromString(value)
    }

    public static func convertFromExtendedGraphemeClusterLiteral(value: StringLiteralType) -> CGPoint {
        return convertFromStringLiteral(value)
    }
}

Now this worked exactly the way I wanted, I had to use strings because simply case Top = CGPoint(x:0,y:1) does not work.

I use it like this ex:

1. var Dir:Direction = .None
2. self.center.AddWithPoint(self.Dir.rawValue.multiply(CGFloat(Speed)))
3. bullet.Dir = .Right

Now thats how Ive used the enum before 8.1, but now it is not longer allowed.

Now I get a ton of errors saying things like this:

. Type 'CGPoint' does not conform to protocol 'StringLiteralConvertible'

. 'String' is not convertible to 'CGPoint'

So is there any way I can make it work as before? Any answer or suggestion are appreciated!

Arbitur
  • 38,684
  • 22
  • 91
  • 128

1 Answers1

4

The StringLiteralConvertible protocol changed and does now require an initializer:

protocol StringLiteralConvertible : ExtendedGraphemeClusterLiteralConvertible {
    typealias StringLiteralType
    init(stringLiteral value: StringLiteralType)
}

In addition, it inherits from

protocol ExtendedGraphemeClusterLiteralConvertible : UnicodeScalarLiteralConvertible {
    typealias ExtendedGraphemeClusterLiteralType
    init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType)
}

which in turn inherits from

protocol UnicodeScalarLiteralConvertible {
    typealias UnicodeScalarLiteralType
    init(unicodeScalarLiteral value: UnicodeScalarLiteralType)
}

So you can implement the protocol as:

extension CGPoint: StringLiteralConvertible {

    public init(stringLiteral value: StringLiteralType) {
        self = CGPointFromString(value)
    }

    public init(extendedGraphemeClusterLiteral value: StringLiteralType) {
        self = CGPointFromString(value)
    }

    public init(unicodeScalarLiteral value: StringLiteralType) {
        self = CGPointFromString(value)
    }
}

Example usage:

 print(Direction.Top.rawValue)
 // Output: (0.0,-1.0)
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382