0

I am making an app where a textfield input is shown on a label for the amount of time (in seconds) indicated on an other textfield. My app only runs when var seconds is set to an integer and not to the textfield input. When I set var seconds to the textfield input, I get the following error on the indicated lines of code: 'ViewController' does not have a member named 'seconds' Can anyone help? Thanks.

Here is all of my code, the lines of code with the error have //Error beside them:

class ViewController: UIViewController {


@IBOutlet weak var chronometre: UILabel!
@IBOutlet weak var prochainpointLabel: UILabel!
@IBOutlet weak var pointactuelLabel: UILabel!
@IBOutlet weak var point1Textfield: UITextField!
@IBOutlet weak var point1tempsTextfield: UITextField!
@IBOutlet weak var point2Textfield: UITextField!
@IBOutlet weak var point2tempsTextfield: UITextField!

var timer = NSTimer()
var seconds = point1tempsTextfield.text.toInt()

func subtractTime(dt:NSTimer){
        seconds-- //Error
        chronometre.text = "\(seconds)" //Error
    if(seconds == 0) { //Error
        seconds = point2tempsTextfield.text.toInt()
        pointactuelLabel.text = point2Textfield.text
        prochainpointLabel.text = nil

    }
}


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.
}





@IBAction func presenterButton(sender: AnyObject) {
    pointactuelLabel.text = point1Textfield.text
    prochainpointLabel.text = point2Textfield.text
    chronometre.text = "\(seconds)" //Error
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: ("subtractTime:"), userInfo: nil, repeats: true)
        }

}

Thank you for your help, I am very new to coding and really appreciate it!

Jacob
  • 23
  • 5

1 Answers1

0

Forget about using a timer to calculate time. You should use NSTimeInterval between two dates to measure elapsed time.

ElapsedTime

extension NSTimeInterval {
    var time:String {
        return String(format:"%02d:%02d", Int((self/60.0)%60), Int((self) % 60 ))
    }
}


var startTime:NSTimeInterval = 0
var elapsedTime:NSTimeInterval = 0

startTime =  NSDate().timeIntervalSinceReferenceDate  // 446644854.4675

sleep(3)   //
elapsedTime = NSDate().timeIntervalSinceReferenceDate - startTime  // 3.005680024623871

println(elapsedTime.time) "00:03"

// applying this concept to your code would look like this:

class ViewController: UIViewController {
    @IBOutlet weak var chronometre: UILabel!
    @IBOutlet weak var prochainpointLabel: UILabel!
    @IBOutlet weak var pointactuelLabel: UILabel!
    @IBOutlet weak var point1Textfield: UITextField!
    @IBOutlet weak var point1tempsTextfield: UITextField!
    @IBOutlet weak var point2Textfield: UITextField!
    @IBOutlet weak var point2tempsTextfield: UITextField!

    var timer = NSTimer()
    var startTime:NSTimeInterval = 0

    func updateTime(){
        chronometre.text = (NSDate().timeIntervalSinceReferenceDate - startTime).time
    }
    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.
    }
    @IBAction func presenterButton(sender: AnyObject) {
        pointactuelLabel.text = point1Textfield.text
        prochainpointLabel.text = point2Textfield.text
        timer = NSTimer.scheduledTimerWithTimeInterval(1/10, target: self, selector: ("updateTime:"), userInfo: nil, repeats: true)
        startTime =  NSDate().timeIntervalSinceReferenceDate  

    }
}
Community
  • 1
  • 1
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • I do not know how to use NSTimeInterval and I cannot find any tutorials. Do you know if there's a website or a video that can tell me? @Leonardo Savio Dabus – Jacob Feb 26 '15 at 03:05
  • As I said earlier, I am new to coding. I do not really understand the code in the link. But is there any way that I can do this with my current NSTimer? @Leonardo Savio Dabus – Jacob Feb 26 '15 at 11:45
  • I have updated the answer with every single step you need to get the elapsed time between two dates (which represents a point in time) – Leo Dabus Feb 26 '15 at 11:57
  • As you can see I asked the computer to sleep for 3 seconds and it will always take a little bit longer to complete the task. Thats why you can't use a timer to chronometer time. It may be a small difference but it will increase with time. If you need further explanation just let me know. – Leo Dabus Feb 26 '15 at 12:03
  • Ok, there's only one error on the following line `chronometre.text = (NSDate().timeIntervalSinceReferenceDate - startTime).time` the error is `'NSTimeInterval' is not convertible to 'UInt8'` @Leonardo – Jacob Feb 26 '15 at 12:41
  • there must be something else wrong NSDate().timeIntervalSinceReferenceDate returns NSTimeInterval (same as Double) – Leo Dabus Feb 26 '15 at 12:43
  • How do I send you the project? @Leonardo – Jacob Feb 26 '15 at 12:45
  • Do you have Dropbox ? – Leo Dabus Feb 26 '15 at 12:46
  • right click your project enclosing folder and select compress\ – Leo Dabus Feb 26 '15 at 12:47
  • this will create a zip file. just post it to www.dropbox.com and share the link – Leo Dabus Feb 26 '15 at 12:48
  • Are you able to fix it? @Leonardo – Jacob Feb 26 '15 at 13:27
  • I could but I wouldn't help you doing it for you I rather try to explain what part of my code you don't understand. This way you wouldn't need help to solve any issue you might encounter ahead. – Leo Dabus Mar 02 '15 at 02:25
  • I only copy/pasted the code above into my project. Perhaps there was something else I was supposed put in my code? @Leonardo – Jacob Mar 04 '15 at 22:09