Swift Converting Character to String

IosSwift

Ios Problem Overview


I have an issue with converting character type to String type. First of all, I have below extension of String for finding nth character within String.

extension String {
    func characterAtIndex(index: Int) -> Character? {
        var cur = 0
        for char in self {
            if cur == index {
                return char
            }
            cur++
        }
        return nil
    }
}

I get what I want with this class extension. However when I use that nth character for title of my custom UIButton, gives an error. My Uibutton Class is

class hareketliHarfler: UIButton {
    init(frame: CGRect) {
        super.init(frame: frame)
        // Initialization code
    }
    func getLetter(letter:String!){
        self.titleLabel.text = letter 
    }
}

The error show when i try to access "getLetter(letter:String)" function. Here is example of main view Controller codes:

    var harfim = hareketliHarfler(frame: CGRectMake(100,100,100,100))
var str="This is my String"
var bufi=str.characterAtIndex(3)
    harfim.getLetter(bufi as AnyObject) ****

In *** section I try .getLetter(bufi), .getLetter(bufi as String) also I try to change parameter type of function. Look like: func getLetter(letter:Character!) or func getLetter(letter:AnyObject!)...etc Didn't find a way. Need a help on that. Thank you

Ios Solutions


Solution 1 - Ios

How about the simple String(theCharacter)

Works in Swift 4 and Swift 5

Solution 2 - Ios

Your problem is quite simple: your characterAtIndex function returns a Character, and self.titleLabel.text is a String. You can't convert between the two implicitly. The easiest way would be to turn the Character into a String using the String initialiser:

// ch will be Character? type.
if let ch = str.characterAtIndex(3) {
    // Initialise a new String containing the single character 'ch'
    harfim.getLetter(String(ch))
} else {
    // str didn't have a third character.
}

Unlike other solutions, this is safe for unusual Unicode characters, and won't initialise a potentially large array or iterate the whole String just to get the third character.

Solution 3 - Ios

Change this:

var bufi=str.characterAtIndex(3)
harfim.getLetter(bufi as AnyObject)

to this:

harfim.getLetter(String(Array(str)[3]))

So what happening here:

  1. we create an array from our string. Array elements are symbols from original string. Such break down correctly tracks symbols that are presented with a sequences of two or more code points. E.g. emoji or flag as noted by @MartinR.

  2. We access element at 4-th position.

Note that as we crate an array from initial string then performance wise is better to use this method only with short strings and avoid it in oft-repeated routines. But in your case it seems to be OK.

Solution 4 - Ios

Can also use Character(text).isNumber if you want to get localised numbers.

Reference: https://developer.apple.com/documentation/swift/character/3127015-isnumber

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAntiokhosView Question on Stackoverflow
Solution 1 - IosNaishtaView Answer on Stackoverflow
Solution 2 - IosMatt GibsonView Answer on Stackoverflow
Solution 3 - IosKeenleView Answer on Stackoverflow
Solution 4 - IosEzzerView Answer on Stackoverflow