2016-04-21 26 views
2

我有一個簡單的標籤文本動畫在Swift中。 我得到了以下錯誤:如何解決:類型'字符串'不符合協議'SequenceType'在Swift

Type 'String' does not conform to protocol '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

另外,你知道每種角色都有不同的顏色嗎?

我希望你能幫助我! 提前謝謝!

+2

[String類型不符合協議序列類型(的可能的複製http://stackoverflow.com/questions/36479826/type-string-does -not-conform-to-protocol-sequencetype) – Thilo

+0

@Thilo,我認爲它有點不同。 –

+0

請參閱「使用字符」下的示例https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html –

回答

15

而不是做for character in inputText {}你應該做

for character in inputText.characters {}

+1

謝謝!有效。 –

相關問題