【发布时间】:2020-10-18 22:21:35
【问题描述】:
目前我正在使用.listStyle(InsetGroupedListStyle()) 修饰符设置listStyle。
struct ContentView: View {
var body: some View {
ListView()
}
}
struct ListView: View {
let data = ["One", "Two", "Three", "Four", "Five", "Six"]
var body: some View {
List {
ForEach(data, id: \.self) { word in
Text(word)
}
}
.listStyle(InsetGroupedListStyle())
}
}
我想在ListView 中创建一个属性来存储ListStyle。问题是ListStyle 是一个协议,我得到:
Protocol 'ListStyle' 只能用作通用约束,因为 它有 Self 或关联的类型要求
struct ContentView: View {
var body: some View {
ListView(listStyle: InsetGroupedListStyle())
}
}
struct ListView: View {
var listStyle: ListStyle /// this does not work
let data = ["One", "Two", "Three", "Four", "Five", "Six"]
var body: some View {
List {
ForEach(data, id: \.self) { word in
Text(word)
}
}
.listStyle(listStyle)
}
}
我看了这个question,但我不知道ListStyle的associatedtype是什么。
【问题讨论】:
标签: ios swift swiftui protocols