2

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!

Razvan Julian
  • 59
  • 3
  • 14
  • 2
    Possible duplicate of [type String does not conform to protocol sequencetype](http://stackoverflow.com/questions/36479826/type-string-does-not-conform-to-protocol-sequencetype) – Thilo Apr 21 '16 at 02:10
  • @Thilo, I think it's a bit more different. – Razvan Julian Apr 21 '16 at 02:15
  • See the example here under "Working with Characters" https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html – William GP Apr 21 '16 at 02:23
  • @WilliamGP, Thank you! I will take a look. – Razvan Julian Apr 21 '16 at 02:32

1 Answers1

19

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

for character in inputText.characters {}

MikaelM
  • 227
  • 4
  • 11