NSTimer.scheduledTimerWithTimeInterval in Swift Playground

前端 未结 3 862
南方客
南方客 2020-12-20 19:25

All the examples I\'ve seen on using the \"NSTimer.scheduledTimerWithTimeInterval\" within Swift show using the \"target: self\" parameter, but unfortunately this doesn\'t w

相关标签:
3条回答
  • 2020-12-20 20:15

    To get this to run directly within a Swift Playground, you need to embed the printFrom1To1000 function within a class and then set an instance of that class to the "target:" parameter instead of using "self".

    Here's a full working example:

    class myClass: NSTimer{
        func printFrom1To1000() {
            for counter in 0...1000 {
                var b = counter
            }
        }
    }
    
    let myClassInstance = myClass()
    
    var timer = NSTimer.scheduledTimerWithTimeInterval(0,
        target: myClassInstance,
        selector: Selector("printFrom1To1000"),
        userInfo: nil,
        repeats: false
    )
    timer.fire()
    
    0 讨论(0)
  • 2020-12-20 20:26

    You really should not be using NSTimer these days. It's consumes a lot of resources, causes unnecessary battery drain, and the API lends itself to ugly code.

    Use dispatch_after() instead:

    dispatch_after(0, dispatch_get_main_queue()) { () -> Void in
      for counter in 0...1000 {
        var b = counter
      }
    }
    

    Of course, since the timer will fire after playground does it's stuff you will need an equivalent of timer.fire() to force the code to execute immediately instead of after a 0 second delay. Here's how that works:

    let printFrom1To1000 = { () -> Void in
      for counter in 0...1000 {
        var b = counter
      }
    }
    
    dispatch_after(0, dispatch_get_main_queue(), printFrom1To1000)
    
    printFrom1To1000()
    
    0 讨论(0)
  • 2020-12-20 20:26

    If you already have an object you are referencing (i.e., updating a label), you can extend that type and use that function as the Selector. I find this easier than creating a whole new class and instantiating a new object from it.

    extension SKLabelNode {
        func updateMe() {
        count++
        label.text = "\(count)"
        }
    }
    
    var timer = NSTimer.scheduledTimerWithTimeInterval(0.25,
        target: label,
        selector: Selector("updateMe"),
        userInfo: nil,
        repeats: true)
    timer.fire()
    
    0 讨论(0)
提交回复
热议问题