1

iOS Noob here - I am trying to run a background Timer in Swift. The below code generates this error message: Thread 1: EXC_BAD_ACCESS (code=1, address=0xd)

It has something to do with userInfo and I've also tried using nil but no success.

@IBAction func startButton(_ sender: AnyObject) {
    timer = Timer.scheduledTimer (
        timeInterval: 0.01, 
        target: Any.self, 
        selector: #selector(ViewController.whileRunning), 
        userInfo: Any?.self, 
        repeats: true
    )
}

Reading the following documentation was not helpful:
https://developer.apple.com/reference/foundation/timer/1412416-scheduledtimer#discussion

The whole point of this is to move an element around my storyboard over time using the x coordinates.

  • 1
    What exactly are you trying to do with `Any.self`? – JAL Nov 10 '16 at 21:05
  • The truth is I don't know how this is supposed to work. I just want the timer to execute a callback or something so I can refresh the UI on a 1/100th interval. – Spencer Wagner Nov 10 '16 at 21:08
  • 1
    `target` should be `self`, `userInfo` should be anything you want to pass to the function that is called every interval. I'll find a good question that explains how timers work. – JAL Nov 10 '16 at 21:10
  • Possible duplicate of [How can I use NSTimer in Swift?](http://stackoverflow.com/questions/24007518/how-can-i-use-nstimer-in-swift) – JAL Nov 10 '16 at 21:10
  • Thank you JAL - I haven't found that Q&A yet. It looks helpful. I'll mark it as answered when I figure this out. – Spencer Wagner Nov 10 '16 at 21:13
  • LOL @ Any?.self – Sethmr Nov 10 '16 at 21:30

1 Answers1

0

I'm having the same problem!

I solved by maintaining a separated array of userInfo that match the indexes of the different timers (in array of timers of course).

Alessio Campanelli
  • 970
  • 11
  • 19