-1

I am building an iPhone app in Xcode using Swift. I am extremely new to Swift and am creating a basic app to start.

So far I have two buttons, when the "Start" button is pressed, the exact time pops up next to the button. Likewise, when the "End" button is pressed, the exact time pops up next to the button.

I have a third button titled "Total time." When clicked, I would like the difference between the two times to pop up.

Start time and end time are in string variables. My total time is in an int variable. It will not allow me to print the total time as an integer in the label.

How do I do this?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Sam
  • 1
  • [Convert Int to String](http://stackoverflow.com/questions/24161336/convert-int-to-string-in-swift) – Russell Austin May 28 '15 at 15:01
  • It is unclear what you are asking here. What does "It will not allow me to print..." mean? What did you try? What result did you expect? What result did you see instead? You're less likely to get a useful answer if we have to guess what you are doing and how it is failing. – Jonah May 28 '15 at 15:02
  • Do you ask for something like that http://rshankar.com/simple-stopwatch-app-in-swift/ ? – Hons May 28 '15 at 15:04
  • Don't store dates/times in NSStrings. Never. Ever. Under any circumstance. – Cyrille May 28 '15 at 15:28

1 Answers1

1

enter image description hereHere's a Playground showing an example of how to get an integer with the number of second between two points in time (instances of NSDate).

Here i simulate the clicks with a dispatch_after block, but the 2 NSDate's should be generated when with the button clicks.

import UIKit
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

// 1st 'time', this NSDate should be created with the click of the 'Start' button
let startTime = NSDate()

// simulate a 2nd 'time' with dispatch_after
dispatch_after(
    dispatch_time(
        DISPATCH_TIME_NOW,
        Int64(2 * Double(NSEC_PER_SEC))
    ),
    dispatch_get_main_queue(), {
        // second time comes after 2 seconds, should be what happens on the 'End button'
        let endTime = NSDate()

        let diff = endTime.timeIntervalSinceDate(startTime) // this is a Float
        // convert to Int using NSNumber
        let number = NSNumber(float: Float(diff))
        let diffInt = number.intValue
    }
)