Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix: Type 'String' does not conform to protocol 'SequenceType' in Swift

I have a simple Label Text Animation in Swift. And I got the following error:

Type 'String' does not conform to protocol 'SequenceType'

Below there are some of my codes:

LabelTextAnimation.swift:

import UIKit

func setTextWithTypeAnimation(inputText: String, interval: NSTimeInterval, label: UILabel) {

    label.text = ""

    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)) {

        for character in inputText {  // 1

            dispatch_async(dispatch_get_main_queue()) {

                label.text = label.text! + String(character)

            }

            NSThread.sleepForTimeInterval(interval)

        }

    }

}

ViewController.swift:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        setTextWithTypeAnimation("This is very cool", interval: 0.13, label: label)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

// 1 is the line with the error to the string: inputText.

Also, do you know a way for each character to have a different color?

I hope you could help me! Thank you in advance!

like image 534
Razvan Julian Avatar asked Nov 29 '25 00:11

Razvan Julian


1 Answers

Instead of doing for character in inputText {} you should do

for character in inputText.characters {}

like image 107
MikaelM Avatar answered Nov 30 '25 15:11

MikaelM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!