本文介绍了使用三个数组填充复杂功能时间线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个数组,这些数组包含用条目填充复杂时间线的数据.

I have three arrays that have the data to populate the complication timeline with entries.

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

When I scroll through time travel, the complication does not change so I know I must be doing something wrong.

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中对象的日期设置时间线条目
  • Loop through the three arrays
  • Set the template with the results of the array loops
  • Set the timeline entry with the date of the object in body2Array

我的控制台上的输出是:

The output on my console is:

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

推荐答案

为什么时间旅行没有按照您预期的方式工作:

时间旅行仅支持 48 小时滑动窗口.复杂服务器之外的任何时间线条目latestTimeTravelDate 将被忽略.

Time travel only supports a 48-hour sliding window. Any timeline entries outside the complication server's latestTimeTravelDate will be ignored.

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

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

You can't time travel over six months ahead to October 1, so your Mar 21 entry would never change to show the October 1 entry.

其他问题:

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

You probably don't mean to iterate through every body object for each header object.

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

You also want to start with an empty entries array within this method, so you're not inadvertently appending to an array with any existing (backwards) timeline entries.

将循环更改为如下所示:

Change your loop to look something like this:

// 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 条目.

This will give you headerArray.count timeline entries, instead of headerArray.count x body1Array.count x body2Array.count entries.

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

You may also want to specify the type of objects in each array so you don't have to constantly use as!. This will provide type safety and let the compiler type-check your code.

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

It also might make your code more readable and maintainable if you keep the data in an array of structs (with header, text, and date properties), instead of using multiple arrays.

这篇关于使用三个数组填充复杂功能时间线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:34