[英]Convert NSTimInterval to Integer Swift
我需要將NSTimeInterval類型的變量(我知道是Double)轉換為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
}
脅迫:
let i = Int(myTimeInterval)
編輯在評論中,正確地指出這種方法可能失敗(並崩潰),因為myTimeInterval
包含的myTimeInterval
可能大於Int的最大大小,從而導致溢出。
在Swift 4之前,處理這種可能性非常困難。 但是Swift 4引入了兩個新的Int初始化器來解決這個問題:
init(exactly:)
- 這是一個可用的初始化程序,因此它返回一個可能的Int,它可能是nil
。
init(clamping:)
- 這總是成功的,因為如果它因溢出而失敗,最大的合法Int被替換。
您可以像這樣將Int值轉換為Int: Int(exampleValue)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.