Created
February 2, 2015 10:57
-
-
Save genedelisa/b40710734f3fd0538c8a to your computer and use it in GitHub Desktop.
Swift StringLiteralConvertible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Foo : StringLiteralConvertible { | |
var num:Int = 0 | |
required public init(stringLiteral value: StringLiteralType) { | |
self.num = value.toInt()! | |
// of course you can do something more complicated by parsing the value. | |
} | |
public typealias UnicodeScalarLiteralType = StringLiteralType | |
convenience required public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) { | |
self.init(stringLiteral: value) | |
} | |
public typealias ExtendedGraphemeClusterLiteralType = StringLiteralType | |
required convenience public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) { | |
self.init(stringLiteral: value) | |
} | |
} | |
var foo:Foo = "1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
StringLiteralConvertible conforms to protocol ExtendedGraphemeClusterLiteralConvertible which in turn conforms to UnicodeScalarLiteralConvertible. That is why you need three inits.