【问题标题】:Using three arrays to populate complication timeline使用三个数组填充并发症时间线
【发布时间】:2016-03-20 21:51:32
【问题描述】:

我有三个数组,它们的数据可以用条目填充并发症时间线。

当我滚动时间旅行时,复杂性没有改变,所以我知道我一定做错了什么。

func getTimelineEntriesForComplication(complication: CLKComplication, afterDate date: NSDate, limit: Int, withHandler handler: (([CLKComplicationTimelineEntry]?) -> Void)) {

    for headerObject in headerArray! {
        for body1Object in body1Array! {
            for body2Object in body2Array! {
                let headerTextProvider = CLKSimpleTextProvider(text: headerObject as! String)
                let body1TextProvider = CLKSimpleTextProvider(text: body1Object as! String)
                let body2TextProvider = CLKRelativeDateTextProvider(date: body2Object as! NSDate, style: .Offset, units: .Day)
                print("HeaderTextProvider: \(headerTextProvider)")
                print("Body1TextProvider: \(body1TextProvider)")
                print("Body2TextProvider: \(body2TextProvider)")
                let template = CLKComplicationTemplateModularLargeStandardBody()
                template.headerTextProvider = headerTextProvider
                template.body1TextProvider = body1TextProvider
                template.body2TextProvider = body2TextProvider
                let timelineEntry = CLKComplicationTimelineEntry(date: body2Object as! NSDate, complicationTemplate: template)
                entries.append(timelineEntry)
                print("TimeEnt: \(entries)")
                print("TimeEntCount: \(entries.count)")
            }
        }
    }
    handler(entries)
}

我的想法:

  • 循环遍历三个数组
  • 使用数组循环的结果设置模板
  • body2Array中的对象日期设置时间线条目

我的控制台上的输出是:

HeaderTextProvider: <CLKSimpleTextProvider: 0x78e3f800>
Body1TextProvider: <CLKSimpleTextProvider: 0x78e4eb30>
Body2TextProvider: <CLKRelativeDateTextProvider: 0x78e4f050>
TimeEnt: [<CLKComplicationTimelineEntry: 0x78e4edd0> date = 2016-03-21 05:00:00 +0000, template = <CLKComplicationTemplateModularLargeStandardBody: 0x78e4edf0>, animationGroup = (null), <CLKComplicationTimelineEntry: 0x78e4f520> date = 2016-10-01 17:00:00 +0000, template = <CLKComplicationTemplateModularLargeStandardBody: 0x78e4f540>, animationGroup = (null)]
TimeEntCount: 2

【问题讨论】:

    标签: swift watchkit watchos-2 apple-watch-complication


    【解决方案1】:

    为什么时间旅行没有按照您的预期进行:

    时间旅行仅支持 48 小时滑动窗口。 the complication server'slatestTimeTravelDate 之外的任何时间线条目都将被忽略。

    在构建您的时间线时,请勿在此日期之后创建任何条目。这样做是浪费时间,因为这些条目不会立即显示。

    您不能将时间旅行提前六个月到 10 月 1 日,因此您 3 月 21 日的条目永远不会更改为显示 10 月 1 日的条目。

    其他问题:

    您可能不是要遍历每个标题对象的每个正文对象。

    您还希望在此方法中从一个空的 entries 数组开始,这样您就不会无意中将任何现有(向后)时间线条目附加到一个数组中。

    将循环更改为如下所示:

    // Unwrap optional arrays.  You can also check counts if there's the possibility that they are not equally sized.
    guard let headers = headerArray, texts = body1Array, dates = body2Array else { return handler(nil) }
    
    var entries = [CLKComplicationTimelineEntry]()
    for (index, header) in headers.enumerate() {
        let text = texts[index]
        let date = dates[index]
        ...
    }
    print("TimeEntCount: \(entries.count)")
    

    这将为您提供 headerArray.count 时间线条目,而不是 headerArray.count x body1Array.count x body2Array.count 条目。

    您可能还想在每个数组中指定对象的类型,这样您就不必经常使用 as!。这将提供类型安全并让编译器对您的代码进行类型检查。

    如果您将数据保存在结构数组(带有标题、文本和日期属性)中,而不是使用多个数组,这也可能使您的代码更具可读性和可维护性。

    【讨论】:

    • 顺便说一句,我唯一没有提到的是将日期与afterDate 进行比较。如果您的数组包含日期可能在之前 afterDate 的数据,则您可能希望跳过该条目而不是在时间轴中返回它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多