将NSTimeInterval转换为整数Swift

14

我需要将一个类型为NSTimeInterval的变量转换为整数(Integer)。我已经尝试过这里的解决方案: 如何将NSTimeInterval转换为int?,但没有成功。有人能否给出在Swift中如何实现此操作的建议?以下是我的函数:

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
}
2个回答

29

强制:

let i = Int(myTimeInterval)

编辑 在评论中,有人指出这种方法可能会失败(并崩溃),因为myTimeInterval中包含的Double可能大于Int的最大大小,导致溢出。

直到Swift 4,处理这种可能性相当困难。但是Swift 4引入了两个新的Int初始化程序来解决这个问题:

  • init(exactly:) — 这是一个可失败的初始化程序,因此它返回一个可选的Int,可能为nil

  • init(clamping:) — 这总是成功的,因为如果由于溢出而失败,最大的合法Int将被替换。


在这种情况下,您会得到致命错误:无法将双精度值转换为Int,因为结果大于Int.max - malex
2
@malex 这个问题在Swift 4中已经通过exactly:clamping:初始化器得到解决。 - matt

4
您可以这样将Double值转换为Int:Int(exampleValue)

1
然后将您的代码发布在您的问题中,这样我们就能看到具体情况。 - Lachezar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接