如何修复:类型'String'不符合Swift中的协议'SequenceType'

时间:2016-04-21 02:08:44

标签: ios string swift animation label

我在Swift中有一个简单的标签文本动画。 我收到以下错误:

  

类型'String'不符合协议'SequenceType'

下面是我的一些代码:

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是字符串错误的行:inputText

另外,你知道每个角色有不同颜色的方法吗?

我希望你能帮助我! 提前谢谢!

1 个答案:

答案 0 :(得分:17)

而不是for character in inputText {}你应该做

for character in inputText.characters {}