Int has a failable
init that takes a
String and attempts to parse an integer value out of it. However, if you have a
Character, you have to build a
String out of it before you can parse out the potential
Int it represents.
let string = "1 is the loneliest number" let loneliestNumber = Int(String(string.first!))
I propose we add a convenience init to Int that takes a Character and a defaulted radix.
convenience init?(asciiDigit character: Character, radix: Int = 10)
let loneliestNumber = Int(asciiDigit: string.first!)
You can also parse textual representations of characters of other radices:
let string = "ABBA is my favorite band!" let oneOfUs = Int(asciiDigit: string.first!, radix: 16)
Make Character conform to
StringProtocol. You cannot (currently) make a type a collection who’s element is
There may be other odd gaps in the API where we currently only take
Strings but it would also make sense to add an overload for
Character, and it would be good to lump those in with this once we get to the proposal stage. If there are any you are aware of, please make it known and we can consider adding them along with this!