Created
May 24, 2015 07:55
-
-
Save cfr/c14dcc9c950872331f30 to your computer and use it in GitHub Desktop.
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
protocol Convertible { | |
func to<T>() -> T; | |
// var <T>to: T { get } // multiple instances of generic var is not allowed | |
} | |
extension String { | |
func to() -> String { | |
return self | |
} | |
func to() -> Int { | |
return (self as NSString).integerValue | |
} | |
func to() -> Double { | |
return (self as NSString).doubleValue | |
} | |
} | |
let s = "5.1" | |
let d: Double = s.to() // 5.1 | |
let i: Int = s.to() // 5 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment