在textView(Swift)中显示数组元素

时间:2015-04-22 17:53:03

标签: ios arrays swift timer

如何将数组中的所有元素添加到延迟1秒的textView中?

语言迅捷 (编程还是比较新的)

2 个答案:

答案 0 :(得分:1)

我认为你需要

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!

    var array = ["string1", "string2", "string3"]
    var i = 0
    var str: String = ""
    var timer = NSTimer()

    func testFunc() {
        str += "\(array[i])\n"
        textView.text = str
        if i == count(array) - 1 {
            timer.invalidate()
        }
        i += 1
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("testFunc"), userInfo: nil, repeats: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

答案 1 :(得分:0)

这是代码,它可以借助DispatchQueue Swift 5帮助延迟1秒

   var new : String = ""
                for a in self.arrayofData{

                    new = new + a + " "
                }


  DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {

  self.mytextView.text = self.new

  }