[英]Convert NSTimInterval to Integer Swift
I need to convert a variable of type NSTimeInterval, which I know is a Double, to an Integer. 我需要将NSTimeInterval类型的变量(我知道是Double)转换为Integer。 I have already tried the solutions here: How to convert NSTimeInterval to int?
我已经尝试过这里的解决方案: 如何将NSTimeInterval转换为int? , with no success.
,没有成功。 Can anyone give any suggestions on how to do this in Swift?
任何人都可以在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. 编辑在评论中,正确地指出这种方法可能失败(并崩溃),因为
myTimeInterval
包含的myTimeInterval
可能大于Int的最大大小,从而导致溢出。
Until Swift 4, dealing with that possibility was quite difficult. 在Swift 4之前,处理这种可能性非常困难。 But Swift 4 introduces two new Int initializers that solve the problem:
但是Swift 4引入了两个新的Int初始化器来解决这个问题:
init(exactly:)
— this is a failable initializer, so it returns an Optional Int which might be nil
. init(exactly:)
- 这是一个可用的初始化程序,因此它返回一个可能的Int,它可能是nil
。
init(clamping:)
— this always succeeds, because if it would fail due to overflow, the largest legal Int is substituted. init(clamping:)
- 这总是成功的,因为如果它因溢出而失败,最大的合法Int被替换。
您可以像这样将Int值转换为Int: Int(exampleValue)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.