gpt4 book ai didi

swift - 将 NSTimInterval 转换为整数 Swift

转载 作者:搜寻专家 更新时间:2023-10-30 22:03:28 24 4
gpt4 key购买 nike

我需要将一个 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
}

最佳答案

强制:

let i = Int(myTimeInterval)

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

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

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

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

关于swift - 将 NSTimInterval 转换为整数 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31617441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com