How to convert an Int to a Character in Swift

23,775

Solution 1

You can't convert an integer directly to a Character instance, but you can go from integer to UnicodeScalar to Character and back again:

let startingValue = Int(("A" as UnicodeScalar).value) // 65
for i in 0 ..< 26 {
    print(Character(UnicodeScalar(i + startingValue)))
}

Solution 2

How to convert an Int to a Character in Swift

For the sake of future visitors, I am providing a basic answer to the question title rather than the details of the question itself.

It is a two step process. Convert the Int to a UnicodeScalar and then convert the UnicodeScalar to a Character.

let myInteger: Int = 97

// convert Int to a valid UnicodeScalar
guard let myUnicodeScalar = UnicodeScalar(myInteger) else {
    return
}

// convert UnicodeScalar to Character
let myCharacter = Character(myUnicodeScalar)

// results
print(myCharacter) // a

(source)

Or alternatively...

if let myUnicodeScalar = UnicodeScalar(97) 
    let myCharacter = Character(myUnicodeScalar)
}

See also

Solution 3

try this

for i in 0...25
{
    let string = String(format: "%c", i+65) as String
    NSLog("%@", string)
}

Solution 4

So far I've come up with this:

for i in 0 ..< 26 {
    print(Character(UnicodeScalar(Int(UnicodeScalar("A").value) + i)))
}

If you're just trying to generate "A" to "Z", you can avoid the math and just do:

for c in UnicodeScalar("A").value...UnicodeScalar("Z").value {
    print(String(UnicodeScalar(c)))
}

Solution 5

Simply convert the integer into String, then convert string into Character

let number = 5
let numChar = Character(String(number))
Share:
23,775
J-Dizzle
Author by

J-Dizzle

Electrical Engineer in the office, Mechanical Engineer in the garage with both rooted deeply in my heart.

Updated on February 10, 2020

Comments

  • J-Dizzle
    J-Dizzle over 4 years

    I've struggled and failed for over ten minutes here and I give in. I need to convert an Int to a Character in Swift and cannot solve it.

    Question

    How do you convert (cast) an Int (integer) to a Character (char) in Swift?

    Illustrative Problem/Task Challenge

    Generate a for loop which prints the letters 'A' through 'Z', e.g. something like this:

        for(var i:Int=0;i<26;i++) {      //Important to note - I know 
            print(Character('A' + i));   //this is horrendous syntax...
        }                                //just trying to illustrate! :)
    
  • J-Dizzle
    J-Dizzle over 8 years
    Ok fine... You are right and I hate that. Done! Here is the C-style if anyone wants it -
  • J-Dizzle
    J-Dizzle over 8 years
    for(var i:Int=0; i<26; i++) { var itemStr:String = String(UnicodeScalar(i + startingValue)); items.append(String(format: "Item '%@'", itemStr)); }
  • J-Dizzle
    J-Dizzle over 8 years
    good catch. I replaced the original "items.append(String(format: "Item '%@'", itemStr))" with your proposal of "items.append("Item " + itemStr)
  • jakehawken
    jakehawken over 4 years
    That UnicodeScalar initializer is optional though, and that Character initializer doesn't take an optional. You have to do something more like Character(UnicodeScalar(i + startingValue)!)