迅速:从数组中多次调用动作

时间:2018-07-24 07:04:07

标签: ios swift

我的数组具有int值:

Result Message: 

    Test method  “Test001” threw exception: 
    System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TestTools.UITest.WindowsStoreUtility, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.=== Pre-bind state information ===
    LOG: DisplayName = Microsoft.VisualStudio.TestTools.UITest.WindowsStoreUtility, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
     (Fully-specified)
    LOG: Appbase = file:///C:/Users/user-name/documents/visual studio 2015/Projects/ bin/Debug
    LOG: Initial PrivatePath = NULL
    Calling assembly : Microsoft.VisualStudio.TestTools.UITest.Extension, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
    ===
    LOG: This bind starts in LoadFrom load context.
    WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
    LOG: Using application configuration file: C:\Users\adan\documents\visual studio 2015\Projects \bin\Debug\App.Config
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Post-policy reference: Microsoft.VisualStudio.TestTools.UITest.WindowsStoreUtility, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
    LOG: The same bind was seen before, and was failed with hr = 0x80070002.

动作:

var array = [1,82,17,29,74,94,76,97,48,78,88,20,31]

我希望对数组中的值调用我的操作。

例如:第一次动作在1秒后调用,下次在82秒后调用,然后在17秒后调用。等等...

如何做到?

2 个答案:

答案 0 :(得分:1)

使用以下代码实现这一目标。

    var count = 0
    let seconds = array[count] // get initial value
    _ = Timer.scheduledTimer(timeInterval: seconds, target: self, selector: #selector(callMethod), userInfo: nil, repeats: false)

@objc func callMethod() {
    print("CallMethod....")
    count += 1
    if count < array.count {
    let nextTime = array[count] // get next time value
    _ = Timer.scheduledTimer(timeInterval: nextTime, target: self, selector: #selector(callMethod), userInfo: nil, repeats: false)
 }
}

答案 1 :(得分:1)

在这种情况下,最好使用Timer。在这里,您可以找到如何在快速Timer Example中使用计时器。在示例的更新方法中,只需重新计划具有新秒数值的计时器即可。