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
}
}
错误:
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
您的函数声称要返回CLKComplicationTemplate
,因此在所有情况下它都必须这样做,否则它将无法返回。如果在任何情况下都不可能返回您所承诺的类型的值,则您唯一的其他合理选择是使程序崩溃,通常是通过调用fatalError
来使程序崩溃。 (从技术上讲,您也可以阻止该函数,以便它永远不会返回,例如通过进入无限循环,但这通常不是有用的解决方案。)
这为您提供了许多前进的选择:
fatalError()
是合理的。例如,访问超出其范围的数组索引是编程错误,并且会导致程序崩溃。永远不要对来自系统外部的数据(例如配置数据)做出反应。fatalError
更可取。CLKComplicationTemplate?
并让调用者决定如何处理。例如,如果在一个空数组上调用.first
,则会得到nil。这不是错误; throws
并在其中抛出错误。这些情况。例如,尝试打开一个不存在的文件将引发错误。是错误的,但这不是编程错误。如果您希望呼叫者了解出了什么问题,这将特别有用。您可以将throws
视为将承诺的返回类型更改为“返回值或引发错误”。