【问题标题】:Timeline Entries for multiple Complication Families多个并发症家族的时间线条目
【发布时间】:2016-03-25 20:07:05
【问题描述】:

当我只有 .ModularLarge 时,我的复杂功能和时间旅行完美运行。

我想添加.ModularSmall 作为选项,所以我尝试用几种不同的方式修改我的代码,但它不再按预期工作。

现在发生的情况是 Time Travel 将适用于从 getTimelineEntriesForComplication 生成的数组中的第一个条目,但在执行 Time Travel 时,下一个条目永远不会出现,因此 Complication 只会停留在第一个条目上。

时间轴 尝试使用if 语句

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

    guard let headers = headerArray, texts = body1Array, dates = body2Array else { return }

    var entries = [CLKComplicationTimelineEntry]()
    for (index, header) in headers.enumerate() {
        let text = texts[index]
        let date1 = dates[index]
        let headerTextProvider = CLKSimpleTextProvider(text: header as! String)
        let body1TextProvider = CLKSimpleTextProvider(text: text as! String)
        let timeTextProvider = CLKTimeTextProvider(date: date1 as! NSDate)
        let template = CLKComplicationTemplateModularLargeStandardBody()
        let template2 = CLKComplicationTemplateModularSmallStackText()
        template.headerTextProvider = headerTextProvider
        template.body1TextProvider = body1TextProvider
        template2.line1TextProvider = headerTextProvider
        template2.line2TextProvider = body1TextProvider
        template.body2TextProvider = timeTextProvider

        if complication.family == .ModularLarge {
            let timelineEntry = CLKComplicationTimelineEntry(date: date1 as! NSDate, complicationTemplate: template)
            entries.append(timelineEntry)
            handler(entries)
        }
        if complication.family == .ModularSmall {
            let timelineEntry = CLKComplicationTimelineEntry(date: date1 as! NSDate, complicationTemplate: template2)
            entries.append(timelineEntry)
            handler(entries)
        }
        else {
            handler(nil)
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

    发生的情况是 Time Travel 将对 getTimelineEntriesForComplication 生成的数组中的第一个条目起作用,但在执行 Time Travel 时,下一个条目永远不会出现,因此 Complication 只会停留在第一个条目上。

    您的getTimelineEntriesForComplication 代码中存在错误。您返回了一个只有一个条目的数组,因为您在 for 循环中返回了

    for (index, header) in headers.enumerate() {
        if complication.family == .ModularLarge {
            let timelineEntry = CLKComplicationTimelineEntry(date: date1 as! NSDate, complicationTemplate: template)
            entries.append(timelineEntry)
            handler(entries)
        }
    }
    

    重组你的代码,如下所示:

    for (index, header) in headers.enumerate() {
        if complication.family == .ModularLarge {
            let timelineEntry = CLKComplicationTimelineEntry(date: date1 as! NSDate, complicationTemplate: template)
            entries.append(timelineEntry)
        }
    }
    handler(entries)
    

    【讨论】:

    • 代码有效,但只是想知道您对if 语句或case switch 是否更适合在方法中使用的意见。然后如果你选择if,你会只使用if还是使用if else
    • 我个人认为switch语句更容易阅读和维护,并且内置了更多的代码安全性。 else if 块可能会发生更多错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    相关资源
    最近更新 更多