swift - 使用三个数组填充复杂时间线

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

我有三个数组,其中包含用条目填充复杂时间线的数据。

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

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

最佳答案

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

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

When constructing your timeline, do not create any entries after this date. Doing so is a waste of time because those entries will not be displayed right away.

您无法时间旅行到 10 月 1 日之前的六个月以上,因此您 3 月 21 日的条目永远不会更改为显示 10 月 1 日的条目。

其他问题:

您可能并不打算遍历每个 header 对象的每个主体对象。

您还希望在此方法中从一个空的 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!。这将提供类型安全并让编译器对您的代码进行类型检查。

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

关于swift - 使用三个数组填充复杂时间线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36120050/

相关文章:

json - Alamofire 错误域=NSURLErrorDomain 代码=-999 "cancelled"

ios - WatchOS 唤醒 iOS 应用程序以使用配对的蓝牙耳机开始录音

ios - 由于应用程序标识符授权,应用程序安装失败

ios - UITextField 触摸时崩溃

ios - 错误 : Runner. xcworkspace 不存在。实现谷歌地图时出错

ios - 我的打印语句都没有出现在 Xcode 控制台中

ios - 在真正的 Apple Watch 上调试

ios - WatchKit 数据不显示

ios - 并发症图像是白框 - watchOS 2 ClockKit

ios - Xcode - 图像在用户选择后未出现在 UIImageView 中