加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将NSTimInterval转换为Integer Swift

发布时间:2020-12-14 05:23:14 所属栏目:百科 来源:网络整理
导读:我需要将NSTimeInterval类型的变量(我知道是Double)转换为Integer.我已经尝试过这里的解决方案: How to convert NSTimeInterval to int?,没有成功.任何人都可以在Swift中提供有关如何执行此操作的任何建议吗? 以下是我的功能: func getHKQuantityData(sam
我需要将NSTimeInterval类型的变量(我知道是Double)转换为Integer.我已经尝试过这里的解决方案: 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,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读