【问题标题】:Convert NSTimInterval to Integer Swift将 NSTimInterval 转换为整数 Swift
【发布时间】:2015-07-24 18:31:49
【问题描述】:

我需要将一个 NSTimeInterval 类型的变量(我知道它是一个 Double)转换为一个整数。我已经在这里尝试了解决方案:How to convert NSTimeInterval to 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
}

【问题讨论】:

    标签: swift nstimeinterval


    【解决方案1】:

    强制:

    let i = Int(myTimeInterval)
    

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

    在 Swift 4 之前,处理这种可能性非常困难。但是 Swift 4 引入了两个新的 Int 初始化器来解决这个问题:

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

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

    【讨论】:

    • 这样可以得到fatal error: Double value cannot be converted to Int because the result would be greater than Int.max
    • @malex 这个问题在 Swift 4 中通过 exactly:clamping: 初始化器解决了。
    【解决方案2】:

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

    【讨论】:

    • 然后在你的问题中发布你的代码,这样我们就可以看到到底发生了什么。
    猜你喜欢
    • 2017-06-04
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多