5 Replies
      Latest reply on Feb 12, 2020 1:30 AM by eskimo
      SyamalaDutt Level 1 Level 1 (0 points)

        Below are the two ways i am doing it, but wanted to know which is best way to do it and why

         

        First way

         

        Timer.scheduledTimer(withTimeInterval: 43200, repeats: true) { timer in
                    let randomNumber = Int.random(in: 1...20)
                    print("Number: \(randomNumber)")
        
                    if randomNumber == 20 {
                        timer.invalidate()
                    }
                }

         

         

        second way

         

         

        let date = Date().addingTimeInterval(43200)
                let timer = Timer(fireAt: date, interval: 0, target: self, selector: #selector(runCode), userInfo: nil, repeats: false)
                RunLoop.main.add(timer, forMode: .common)
        @objc func runCode()
                   {
                       print("Running code after every 5 seconds")
                   }