'characters' is unavailable: Please use String directly . I have a problem with this

1.8k Views Asked by At
private func setTextWithTypingAnimation(_ typedText: String, _ attributes: Dictionary<NSAttributedString.Key, Any>?, _ charInterval: TimeInterval, _ initial: Bool, _ dispatchID: Int) {

        guard typedText.characters.count > 0 && currentDispatchID == dispatchID else {
            typingOver = true
            typingStopped = false
            return
        }

        guard typingStopped == false else {
            stoppedSubstring = typedText
            return
        }

        if initial == true {
            super.text = ""
        }

        let firstCharIndex = typedText.characters.index(typedText.startIndex, offsetBy: 1)
3

There are 3 best solutions below

0
shraddha11 On

character is deprecated. You can use string directly like this.

guard typedText.count > 0 else {
    return
}

let firstCharIndex = typedText.index(typedText.startIndex, offsetBy: 1)
0
Kishan Bhatiya On

As Swift 4 introduced changes on string API.

You can use typedText.isEmpty instead of typedText.characters.count > 0 and for

let firstCharIndex = typedText.index(typedText.startIndex, offsetBy: 1)
0
Jorge Zapata On

Use a for in loop. For example:

let users = ["Jorge", "John", "Jose"]

for user in users {
    print(user)
}

Your output should be:

Jorge
John
Jose