将NSTimInterval转换为整数Swift
我需要将NSTimeInterval类型的变量(我知道是双精度变量)转换为整数。我已经在这里尝试了解决方案,但没有成功。有人能就如何在Swift中实现这一点提出建议吗? 以下是我的职责:将NSTimInterval转换为整数Swift,swift,nstimeinterval,Swift,Nstimeinterval,我需要将NSTimeInterval类型的变量(我知道是双精度变量)转换为整数。我已经在这里尝试了解决方案,但没有成功。有人能就如何在Swift中实现这一点提出建议吗? 以下是我的职责: func getHKQuantityData(sampleType: HKSampleType, timeUnit: NSCalendarUnit, startDate: NSDate, endDate: NSDate, completion: (Void -> Void)) -> [(NSDate
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
}
胁迫:
let i = Int(myTimeInterval)
EDIT在一篇评论中,有人正确地指出,这种方法可能会失败(并崩溃),因为myTimeInterval
中包含的Double可能大于Int的最大大小,从而导致溢出
在Swift 4之前,处理这种可能性相当困难。但是Swift 4引入了两个新的Int初始值设定项来解决这个问题:
-这是一个可失败的初始值设定项,因此它返回一个可选的Int,可能是init(正好:)
nil
-这总是成功的,因为如果由于溢出而失败,将替换最大的合法Intinit(钳制:)
Int(exampleValue)
然后在问题中发布您的代码,这样我们就可以看到到底发生了什么。通过这种方式,您可以得到致命错误:无法将双精度值转换为Int,因为结果将大于Int.max
@malex该问题在Swift 4中通过精确:
和夹紧:
初始化器得到解决。