大佬教程收集整理的这篇文章主要介绍了将NSTimInterval转换为Integer 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,options: .None) repeat = timeInterval // 2. Case for minutes case 61...3599: predicate = HKQuery.predicateForSamplesWithStartDate(NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitMinute,endDate: NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitMinute,options: .None) repeat = round(timeInterval / 60) // 3. Case for Hours case 3600...86399: predicate = HKQuery.predicateForSamplesWithStartDate(NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitHour,endDate: NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitHour,options: .None) repeat = round(timeInterval / 3600) // 4. Default for Days default: predicate = HKQuery.predicateForSamplesWithStartDate(NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay,endDate: NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay,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(完全:) – 这是一个可用的初始化程序,因此它返回一个可能的Int,它可能是nil.> init(clamp
以上是大佬教程为你收集整理的将NSTimInterval转换为Integer Swift全部内容,希望文章能够帮你解决将NSTimInterval转换为Integer Swift所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。