-1

I want to use the init(fireAt:interval:target:selector:userInfo:repeats:) timer to call a specific function at a specific time. But the problem is that i don't know how to initialize it. in the documentation it says that:

'You must add the new timer to a run loop, using add(_:forMode:)' (https://developer.apple.com/reference/foundation/timer/1415700-init)

But i am not sure how i go about doing this. Can anybody explain it for me?

1 Answers1

0

Try using the class function scheduledTimerWithTimeInterval.

Swift 3

var calender = Calendar(identifier: .gregorian)

calender.timeZone = TimeZone(abbreviation: "GMT")!

let tomorrow = calender.date(byAdding: .day, value: 1, to: Date())
let midnight = calender.startOfDay(for: tomorrow!)

let timer = Timer(
    fireAt: midnight,
    interval: 5,
    target: self,
    selector: #selector(didInterval),
    userInfo: nil,
    repeats: false
)
Callam
  • 11,409
  • 2
  • 34
  • 32
  • I should probably have been more clear about the specific use. I wan't to call a function at a specific time (midninght). I was thinking that this is what the timer with (fireAt:) is for? – Nicolai Elhøj Nielsen Oct 26 '16 at 22:03