2
votes

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!

1
@Thilo, I think it's a bit more different.Razvan Julian
See the example here under "Working with Characters" developer.apple.com/library/ios/documentation/Swift/Conceptual/…William GP
@WilliamGP, Thank you! I will take a look.Razvan Julian

1 Answers

18
votes

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

for character in inputText.characters {}