我不知道Swift返回值是什么

问题描述 投票:-2回答:2

Xcode说输入一个返回值,但我不知道要使用什么返回值。

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
    let entry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: constructTemplate(for: complication))
    handler(entry)
}

func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
    handler(constructTemplate(for: complication))
}

private func constructTemplate(for complication: CLKComplication) -> CLKComplicationTemplate {
    switch complication.family {
        case .modularSmall:
            let template = CLKComplicationTemplateModularSmallSimpleText()
            let provider = CLKSimpleTextProvider(text: GioTexts.TitleLeft.rawValue)
            template.textProvider = provider

            return template
        case .modularLarge:
            let t = CLKComplicationTemplateModularLargeStandardBody()
            t.headerImageProvider = CLKImageProvider(onePieceImage: UIImage(named: "Complication/Circular")!)
            t.headerTextProvider = CLKSimpleTextProvider(text: GioTexts.TitleLeft.rawValue)
            t.body1TextProvider = CLKSimpleTextProvider(text: GioTexts.SubtitleLeft.rawValue)
            t.body2TextProvider = CLKSimpleTextProvider(text: GioTexts.SubtitleRight.rawValue)
            return t
        case .extraLarge:
            let t = CLKComplicationTemplateExtraLargeColumnsText()
            t.row1Column2TextProvider = CLKSimpleTextProvider(text: GioTexts.TitleLeft.rawValue)
            t.row1Column1TextProvider = CLKSimpleTextProvider(text: "")
            t.row2Column2TextProvider = CLKSimpleTextProvider(text: GioTexts.SubtitleLeft.rawValue)
            t.row2Column1TextProvider = CLKSimpleTextProvider(text: GioTexts.SubtitleRight.rawValue)
            t.column2Alignment = .trailing
            return t
        case .utilitarianSmallFlat, .utilitarianSmall:
            let t = CLKComplicationTemplateUtilitarianSmallFlat()
            t.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "Complication/Circular")!)
            t.textProvider = CLKSimpleTextProvider(text: GioTexts.TitleLeft.rawValue)
            return t
        case .utilitarianLarge:
            let t = CLKComplicationTemplateUtilitarianLargeFlat()
            t.textProvider = CLKSimpleTextProvider(text: GioTexts.TitleLeft.rawValue)
            return t
        case .circularSmall:
            let t = CLKComplicationTemplateCircularSmallStackImage()
            t.line1ImageProvider = CLKImageProvider(onePieceImage: UIImage(named: "Complication/Circular")!)
            t.line2TextProvider = CLKSimpleTextProvider(text: GioTexts.TitleLeft.rawValue)
            return t
        case .graphicCorner: break
        case .graphicBezel: break
        case .graphicCircular: break
        case .graphicRectangular: break

    }
}

错误:

screenshot

swift watch-os clockkit
2个回答
0
投票

你的功能声称返回一个CLKComplicationTemplate,所以在所有情况下它必须这样做,否则它不能返回。如果在某些情况下无法返回您承诺的类型的值,那么您唯一的合理选择就是使程序崩溃,最常见的是调用fatalError。 (从技术上讲,你也可以阻止这个函数,所以它永远不会返回,例如进入一个无限循环,但这通常不是一个有用的解决方案。)

这为您提供了许多如何前进的选择:

  • 如果有一些合理的默认值,您可以返回未处理的并发症。
  • 如果您考虑将无效并发症传递给此函数作为编程错误,那么在这些情况下调用fatalError()是合理的。例如,访问其边界之外的数组索引是一个编程错误,并以这种方式崩溃程序。您永远不应该这样做以响应来自系统外部的数据(例如配置数据)。
  • 您可以更改此方法以接受仅包含您支持的案例的枚举,而不是传递整个并发症。这使得调用它不正确,并且通常比调用fatalError更可取。
  • 如果传递无效的并发症是可以接受的,但是应该什么也不做,那么你应该改变方法以返回CLKComplicationTemplate?并让调用者决定如何处理它。例如,如果在空数组上调用.first,则为零。这不是错误;没有这样的元素。
  • 如果传递无效的并发症是一个错误,但应该由调用者处理(例如,如果它来自配置数据),那么您应该更改此方法以添加throws并在这些情况下抛出错误。例如,尝试打开不存在的文件会引发错误。这是错的,但这不是编程错误。如果您希望调用者了解出错的地方,这将特别有用。您可以将throws视为将承诺的返回类型更改为“返回值或抛出错误”。

0
投票

你的方法被定义为返回CLKComplicationTemplate,但你的最后四个case条款只是break,没有返回任何东西。

由于使用此实用程序方法的CLKComplicationDataSource方法都接受选项,因此您应该只定义此方法以返回可选项(即CLKComplicationTemplate?)并让这四种情况返回nil

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
    let entry = constructTemplate(for: complication).flatMap {
        CLKComplicationTimelineEntry(date: Date(), complicationTemplate: $0)
    }
    handler(entry)
}

func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
    handler(constructTemplate(for: complication))
}

private func constructTemplate(for complication: CLKComplication) -> CLKComplicationTemplate? {
    switch complication.family {
        case .modularSmall:
            ...
        case .modularLarge:
            ...
        case .extraLarge:
            ...
        case .utilitarianSmallFlat, .utilitarianSmall:
            ...
        case .utilitarianLarge:
            ...
        case .circularSmall:
            ...
        case default:
            return nil
    }
}   
© www.soinside.com 2019 - 2024. All rights reserved.