如何修复:类型 'String' 不符合 Swift 中的协议 'SequenceType'
How to fix: Type 'String' does not conform to protocol 'SequenceType' in Swift
我在 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
.
还有,你知道让每个字符有不同颜色的方法吗?
希望你能帮助我!
提前致谢!
你应该做
而不是 for character in inputText {}
for character in inputText.characters {}
我在 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
.
还有,你知道让每个字符有不同颜色的方法吗?
希望你能帮助我! 提前致谢!
你应该做
而不是for character in inputText {}
for character in inputText.characters {}