2

I have come across a lot of questions similar to this, but many were for older versions of Xcode, or simply did not work. I'm using Xcode Version 8.3.2 (8E2002) and Swift coding language. I don't know much about coding, but am young and eager to learn! I'm creating a clicker game that will give you money per second that you are on the game itself. So if you idle for 2 minutes, it would give you $120 ($1per second @120 sec). In addition to this, you also can earn money from clicking the main object. Here is my coding so far:

import UIKit

class ViewController: UIViewController {

var score = 0
var add = 1

func addpersec() {

    score += 1
}
//func used to add to the score based timer. Aka, adding 1 per second



@IBOutlet weak var scorecount: UILabel!


@IBAction func clicks(_ sender: Any) {
    score += 1
    scorecount.text = "Honey: \(score)"
}

@IBOutlet weak var Bees: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
Traxido
  • 71
  • 10

2 Answers2

5
class ViewController: UIViewController {
    var timer: Timer? = nil // Property

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(handleTimer), userInfo: nil, repeats: true)
    }

    func handleTimer(_ timer: Timer) {
        print("Timer ticking!")
    }
}

To invalidate the timer, call self.timer?.invalidate()

Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30
  • Xcode states that 'NSTimer' has been renamed 'Timer' – Traxido May 31 '17 at 02:33
  • @Traxido use `Timer` then. It used to be `NSTimer` but lots of classes have been being renamed by removing the prefixes (like `NS`), which come from Objective-C. – Alejandro Iván May 31 '17 at 02:34
  • Thankyou, I'm new to Xcode and swift so I don't completely understand everything, but another error showed up which states "use of unresolved identifier 'YourViewControllerSubclass'" – Traxido May 31 '17 at 02:36
  • @Traxido understandable. `YourViewControllerSubclass` is the subclass of `UIViewController` you're editing right now (i.e. it **is** your view controller). In your case, your subclass is `ViewController`. Replace all `YourViewControllerSubclass` for `ViewController` and that should do it. – Alejandro Iván May 31 '17 at 02:37
  • Last thing I hope, Sorry Again, but this time it states view controller has no member 'handleTimer' – Traxido May 31 '17 at 02:40
  • @Traxido I would recommend you watching the Stanford University videos about iOS programming (available for free on iTunes U). By watching the first four or five videos will set you ready to do more complex things. – Alejandro Iván May 31 '17 at 02:40
  • Your `selector` doesn't match your function. Change your function to `func handleTimer(_ timer: Timer)`. Also, you can just use the shorthand `#selector(handleTimer)` as the selector. – vacawama May 31 '17 at 02:42
  • Modified the answer to include more code (and fixed some typos). @vacawama thanks for your pointer, you're right. – Alejandro Iván May 31 '17 at 02:49
  • Here is what I now have `import UIKit class ViewController: UIViewController { var score = 0 var add = 1 let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.handleTimer(_:)), userInfo: nil, repeats: true) func handleTimer(_ timer: Timer) { score += 1 } ` But now I get a sigabrt :( – Traxido May 31 '17 at 02:49
  • @Traxido watch the modified answer, it should be more clarifying now. – Alejandro Iván May 31 '17 at 02:53
  • @Traxido accept the answer if it helped you, please. – Alejandro Iván Jun 01 '17 at 15:55
0

Your question seems to be related to iOS UI, so I don't know if my answer makes sense.

For general purpose delayed function execution (like Javascript's setTimeout), you can use a DispatchQueue

// have this as a global somewhere
let bg = DispatchQueue(label: "bg", qos: .background, target: nil)

// anywhere else in your code:

// First decide the time to execute your function
let delayInSeconds = 3
let when = DispatchTime.now() + delayInSeconds

// call it
bg.asyncAfter(deadline: when) {
    // code to execute later (this is the body of an anonymous function)
}
hasen
  • 161,647
  • 65
  • 194
  • 231