I need to convert a variable of type NSTimeInterval, which I know is a Double, to an Integer. I have already tried the solutions here: How to convert NSTimeInterval to int? , with no success. Can anyone give any suggestions on how to do this in Swift? Below is my function:
func getHKQuantityData(sampleType: HKSampleType, timeUnit: NSCalendarUnit, startDate: NSDate, endDate: NSDate, completion: (Void -> Void)) -> [(NSDate, Double)] {
var startTime = 0
var endTime = 0
var repeat: NSTimeInterval
var returnValue: [(NSDate, Double)] = []
var queryType: String = ""
var predicate: NSPredicate!
let timeInterval = endDate.timeIntervalSinceDate(startDate)
switch sampleType {
case HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount):
queryType = "step"
case HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight):
queryType = "height"
case HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass):
queryType = "weight"
case HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex):
queryType = "bmi"
default:
println("No recognized type")
}
switch timeInterval {
// 1. Case for seconds
case 0...59:
predicate = HKQuery.predicateForSamplesWithStartDate(NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitSecond, value: startTime, toDate: NSDate(), options: nil), endDate: NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitSecond, value: startTime, toDate: NSDate(), options: nil), options: .None)
repeat = timeInterval
// 2. Case for minutes
case 61...3599:
predicate = HKQuery.predicateForSamplesWithStartDate(NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitMinute, value: startTime, toDate: NSDate(), options: nil), endDate: NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitMinute, value: startTime, toDate: NSDate(), options: nil), options: .None)
repeat = round(timeInterval / 60)
// 3. Case for Hours
case 3600...86399:
predicate = HKQuery.predicateForSamplesWithStartDate(NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitHour, value: startTime, toDate: NSDate(), options: nil), endDate: NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitHour, value: startTime, toDate: NSDate(), options: nil), options: .None)
repeat = round(timeInterval / 3600)
// 4. Default for Days
default:
predicate = HKQuery.predicateForSamplesWithStartDate(NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: startTime, toDate: NSDate(), options: nil), endDate: NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: startTime, toDate: NSDate(), options: nil), options: .None)
repeat = round(timeInterval / 86400)
}
/*
for x in 1...repeat {
}
*/
// Returns one data point for each timeUnit between startDate and endDate
// array of tuples - (date, double)
return returnValue
}
Coerce:
let i = Int(myTimeInterval)
EDIT In a comment, it is rightly pointed out that this approach can fail (and crash), because the Double contained in myTimeInterval
might be larger than the maximum size of an Int, causing overflow.
Until Swift 4, dealing with that possibility was quite difficult. But Swift 4 introduces two new Int initializers that solve the problem:
init(exactly:)
— this is a failable initializer, so it returns an Optional Int which might be nil
.
init(clamping:)
— this always succeeds, because if it would fail due to overflow, the largest legal Int is substituted.