1. 程式人生 > 其它 >SwiftUI 如何解決Generic parameter ‘SelectionValue‘ could not be inferred

SwiftUI 如何解決Generic parameter ‘SelectionValue‘ could not be inferred

技術標籤:SwiftUI原始碼大全

問題

SwiftUI 如何解決Generic parameter ‘SelectionValue’ could not be inferred

解決方案

可通過設定optional來解決

參考程式碼

struct AuthorView: View {

let authors = ["a","b","c"]
@State private var selectedAuthor: String? 

var body: some View {
    VStack {
        Text("Authors").font(.title)
        HStack {
            List(authors, id: \.self, selection: $selectedAuthor) {author in
                Text(author).tag(author)
            }
            Spacer()
        }
    }
}

技術交流

QQ:3365059189
SwiftUI技術交流QQ群:518696470