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
}
}
错误:
你的功能声称返回一个CLKComplicationTemplate
,所以在所有情况下它必须这样做,否则它不能返回。如果在某些情况下无法返回您承诺的类型的值,那么您唯一的合理选择就是使程序崩溃,最常见的是调用fatalError
。 (从技术上讲,你也可以阻止这个函数,所以它永远不会返回,例如进入一个无限循环,但这通常不是一个有用的解决方案。)
这为您提供了许多如何前进的选择:
fatalError()
是合理的。例如,访问其边界之外的数组索引是一个编程错误,并以这种方式崩溃程序。您永远不应该这样做以响应来自系统外部的数据(例如配置数据)。fatalError
更可取。CLKComplicationTemplate?
并让调用者决定如何处理它。例如,如果在空数组上调用.first
,则为零。这不是错误;没有这样的元素。throws
并在这些情况下抛出错误。例如,尝试打开不存在的文件会引发错误。这是错的,但这不是编程错误。如果您希望调用者了解出错的地方,这将特别有用。您可以将throws
视为将承诺的返回类型更改为“返回值或抛出错误”。你的方法被定义为返回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
}
}