创建Time Interval Swift列表

时间:2015-10-19 08:41:06

标签: ios swift time intervals

您好我无法创建从到达另一个时间开始的常规时间间隔列表。你能帮助我吗? (Swift 2.0)

示例:

我有两次(8:00 - 23:00)。我知道间隔的持续时间(30分钟)。范围是创建一个时间间隔数组(8:00-8:30,8:00-9:00等)。我使用对象调用Time(start:Int,end:Int)

谢谢

3 个答案:

答案 0 :(得分:5)

您可以创建这样的类来生成类似的区间,

struct Time {

    let start: NSTimeInterval
    let end: NSTimeInterval
    let interval: NSTimeInterval

    init(start: NSTimeInterval, interval: NSTimeInterval, end: NSTimeInterval) {
        self.start = start
        self.interval = interval
        self.end = end
    }

    init(startHour: NSTimeInterval, intervalMinutes: NSTimeInterval, endHour: NSTimeInterval) {
        self.start = startHour * 60 * 60
        self.end = endHour * 60 * 60
        self.interval = intervalMinutes * 60
    }

    var timeRepresentations: [String] {
        let dateComponentFormatter = NSDateComponentsFormatter()
        dateComponentFormatter.unitsStyle = .Positional
        dateComponentFormatter.allowedUnits = [.Minute, .Hour]

        let dateComponent = NSDateComponents()
        return timeIntervals.map { timeInterval in
            dateComponent.second = Int(timeInterval)
            return dateComponentFormatter.stringFromDateComponents(dateComponent)!
        }
    }

    var timeIntervals: [NSTimeInterval]{
       return Array(start.stride(through: end, by: interval))
    }
}



let time = Time(startHour: 8, intervalMinutes: 30, endHour: 23)

print(time.timeRepresentations)

这就产生了,

  

[“8:00”,“8:30”,“9:00”,“9:30”,“10:00”,“10:30”,“11:00”,“11:30” ”   “12:00”,“12:30”,“13:00”,“13:30”,“14:00”,“14:30”,“15:00”,   “15:30”,“16:00”,“16:30”,“17:00”,“17:30”,“18:00”,“18:30”,   “19:00”,“19:30”,“20:00”,“20:30”,“21:00”,“21:30”,“22:00”,   “22:30”,“23:00”]

要检索范围,请将此代码放在最后

let time = Time(startHour: 8, intervalMinutes: 30, endHour: 23)

let ranges = time.timeRepresentations.enumerate().flatMap { index, value -> String? in
    if index + 1 < time.timeRepresentations.count {
        return value + " - " + time.timeRepresentations[index + 1]
    }
    return nil
}

结果将是,

  

[“8:00 - 8:30”,“8:30 - 9:00”,“9:00 - 9:30”,“9:30 - 10:00”,“10:00 -   10:30“,”10:30-11:00“,”11:00-11:30“,”11:30-12:00“,”12:00 -   12:30“,”12:30-13:00“,”13:00-13:30“,”13:30-14:00“,”14:00“ -   14:30“,”14:30-15:00“,”15:00-15:30“,”15:30-16:00“,”16:00 -   16:30“,”16:30-17:00“,”17:00-17:30“,”17:30-18:00“,”18:00“ -   18:30“,”18:30-19:00“,”19:00-19:30“,”19:30-20:00“,”20:00 -   20:30“,”20:30-21:00“,”21:00-21:30“,”21:30-22:00“,”22:00“ -   22:30“,”22:30 - 23:00“]

答案 1 :(得分:0)

您可以在viewDidLoad方法中创建这样的计划计时器,但首先要为方法调用定义一个选择器,

    let mySelector: Selector = "time:end:"
    NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: mySelector, userInfo: nil, repeats: true)

然后定义你的时间方法

func time(start: Int, end: Int) {
    // code goes here, this method will called every 30 minutes.
}

所以现在你可以创建一个列表并添加开始和结束参数。

答案 2 :(得分:0)

包含起始元组

的简单数组
let startDate = NSDate()
let endDate = NSDate()

var intervalsContainer = [(start: NSDate, end: NSDate)]()

var intervalDate = startDate;


var dateComponents30Min = NSDateComponents()
dateComponents30Min.minute = 30

let calendar = NSCalendar.currentCalendar()

while(endDate.compare(intervalDate) == NSComparisonResult.OrderedDescending) {
    let finDate = calendar.dateByAddingComponents(dateComponents30Min, toDate: intervalDate, options: [])!
    intervalsContainer.append((intervalDate,finDate))
    intervalDate = finDate
}