Skip to content

Instantly share code, notes, and snippets.

@iamatypeofwalrus
Created July 21, 2014 18:16
Show Gist options
  • Save iamatypeofwalrus/6a1654e09f31db5f002a to your computer and use it in GitHub Desktop.
Save iamatypeofwalrus/6a1654e09f31db5f002a to your computer and use it in GitHub Desktop.
A convoluted way to add an Integer to a Character and get another Character in Swift
// Per the BNR iOS programming guide's chapter on creating BNRItem
import Cocoa
import Foundation
extension Character {
func unicodeIntValue() -> UInt32
{
let str = String(self)
return str.unicodeScalars[str.unicodeScalars.startIndex].value
}
func addIntToCharacter(value: UInt32) -> Character
{
let old_int = self.unicodeIntValue()
return Character(UnicodeScalar(old_int + value))
}
}
let thing = Character("A")
thing.addIntToCharacter(UInt32(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment