从plist swift加载字符串

时间:2016-07-07 17:22:50

标签: swift plist

我正在尝试使用下面的代码从plist加载一个字符串,然后我想在打字机动画中使用它。但我没能将字符串转换为数组。我该如何转换它? 行var myText = Array(Data [0] .characters)给出错误。

class ViewController: UIViewController {
var Data = [String]()


override func viewDidLoad() {
        super.viewDidLoad()
        let word = NSBundle.mainBundle().pathForResource("Chapter 1", ofType: "plist")
        let dict = NSDictionary(contentsOfFile: word!)

        Data = dict!.objectForKey("Chapter 1") as! [String]

    }

@IBOutlet weak var myTypeWriter: UILabel!



    var myText = Array(Data[0].characters)
    var myCounter = 0
    var timer:NSTimer?

    func fireTimer(){
        timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: #selector(ViewController.typeLetter), userInfo: nil, repeats: true)
    }
    func typeLetter(){
        if myCounter < myText.count {
            myTypeWriter.text = myTypeWriter.text! + String(myText[myCounter])
            let randomInterval = Double((arc4random_uniform(8)+1))/100
            timer?.invalidate()
            timer = NSTimer.scheduledTimerWithTimeInterval(randomInterval, target: self, selector: #selector(ViewController.typeLetter), userInfo: nil, repeats: false)
        } else {
            timer?.invalidate()
        }
        myCounter += 1
    }

0 个答案:

没有答案