I'm working on a library, so I want it to be as easily configurable as possible. I put all the configurable options inside SupportOptions
.
struct SupportOptions {
var text: String = ""
var listStyle: CustomListStyle = CustomListStyle.insetGroupedListStyle
}
extension SupportOptions {
/**
Enum wrapper for `ListStyle`
*/
enum CustomListStyle {
case defaultListStyle
case plainListStyle
case groupedListStyle
case insetGroupedListStyle
case insetListStyle
case sidebarListStyle
}
}
I'm following @Sweeper's suggestion to use an enum to store ListStyle
, so that's what CustomListStyle
is for. But I need to translate CustomListStyle
to an actual ListStyle
. This is what I have so far:
extension List {
func getStyle<S>(listStyle: SupportOptions.CustomListStyle) -> S {
switch listStyle {
case .defaultListStyle:
return DefaultListStyle() as! S
case .plainListStyle:
return PlainListStyle() as! S
case .groupedListStyle:
return GroupedListStyle() as! S
case .insetGroupedListStyle:
return InsetGroupedListStyle() as! S
case .insetListStyle:
return InsetListStyle() as! S
case .sidebarListStyle:
return SidebarListStyle() as! S
}
}
}
But when I use it, I get
Generic parameter 'S' could not be inferred
struct ContentView: View {
let options = SupportOptions(listStyle: .defaultListStyle)
let data = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen"]
var body: some View {
List { /// Error! Generic parameter 'S' could not be inferred
ForEach(data, id: .self) { word in
Text(word)
}
}
.getStyle(listStyle: options.listStyle)
}
}
I tried adding a placeholder type S
, but the error stayed the same...
struct ContentView<S>: View where S: ListStyle {
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…