SwiftUI错误:“视图”。类型'无法转换为'((字符串,字符串,字符串,字形)->“视图”

时间:2020-03-19 15:40:59

标签: swiftui swiftui-navigationlink

第一个SwiftUI项目:) 我在顶层视图的列表中有一个NavLink,它调用了详细视图。 直到我需要在细节视图中添加Struct的@State var之前,它一直起作用,现在调用参数列表与编译器期望的不匹配:

    ZStack {
        VStack {
            NavigationView {
                    List(gList) { gard in
                        NavigationLink(destination:
                            SelectGlyph(catList: gard.category,
                                        firstGlyph: gard.ex1,
                                        descr: gard.title,
                                        selectedGlyph: Glyph() )
                        {

细节视图开始:

struct SelectGlyph: View {
    var catList: String
    var firstGlyph: String
    var descr: String
    @State var selectedGlyph:Glyph = Glyph()

    var body: some View {
        ZStack{
            VStack() {
                VStack {
                    VStack {
                        VStack {

和字形结构定义为:

struct Glyph: Codable, Identifiable, Equatable {
    var id:String {
        return Gardiner
    }
    var Hieroglyph: String
    var Gardiner: String
    let Unicode: String
    let Description: String
    let Transliteration: String
    let Phonetic: String
    let Notes: String

    init() {
        self.Hieroglyph = ""
        self.Gardiner = ""
        self.Unicode = ""
        self.Description = ""
        self.Transliteration = ""
        self.Phonetic = ""
        self.Notes = ""
    }

    init(Hiero:String, Gard:String, Uni:String, Desc:String, trans:String, phon:String, notes:String) {
        self.Hieroglyph = Hiero
        self.Gardiner = Gard
        self.Unicode = Uni
        self.Description = Desc
        self.Transliteration = trans
        self.Phonetic = phon
        self.Notes = notes
    }
}

该错误位于顶级ContentView navLink目标中:

'SelectGlyph.Type' is not convertible to '(String, String, String, Glyph) -> SelectGlyph'

我在呼叫列表中尝试了几种变体,并尝试删除SelectGlyph中的@State包装器,所有操作均无效。任何帮助,将不胜感激!如果我知道如何在View中指定局部变量而不将其作为调用中的必需参数,那将是很好的(我肯定在这里遗漏了一些东西!)

作为对Asperi的响应,我为SelectGlyph视图尝试了显式初始化程序,但在同一位置仍然遇到相同的错误:

struct ContentView: View {
    var gList: [GardinerList] = gardinerTest
    var body: some View {
        UINavigationBar.appearance().backgroundColor = .yellow
            return
        ZStack {
            VStack {
                NavigationView {
                        List(gList) { gard in
                            NavigationLink(destination:
                                SelectGlyph(catList: gard.category,  <- Error Here
                                            firstGlyph: gard.ex1,
                                            descr: gard.title,
                                            selectedGlyph: Glyph() )
                            {
                            Text(gard.category)
                                .fontWeight(.bold)
                                .foregroundColor(Color.green)
带有SelectGlyph视图的

现在已修改为具有显式的init()。我尝试在init()中将selectedGlyph指定为_selectedGlyph和self._selectedGlyph

struct SelectGlyph: View {
    var catList: String
    var firstGlyph: String
    var descr: String
    @State private var selectedGlyph:Glyph = Glyph()

    init(catList: String, firstGlyph: String, descr: String, selectedGlyph:Glyph) {
        self.catList = catList
        self.firstGlyph = firstGlyph
        self.descr = descr
        self._selectedGlyph = State<Glyph>(initialValue: selectedGlyph)
    }

    var body: some View {
        ZStack{
            VStack() {

1 个答案:

答案 0 :(得分:1)

在这里

                NavigationLink(destination:
                    SelectGlyph(catList: gard.category,
                                firstGlyph: gard.ex1,
                                descr: gard.title,
                                selectedGlyph: Glyph() )

您需要在此处使用显式的初始化程序,例如

struct SelectGlyph: View {
    var catList: String
    var firstGlyph: String
    var descr: String

    @State private var selectedGlyph:Glyph // << always keep @State private !!

    init(catList: String, firstGlyph: String, descr: String, selectedGlyph:Glyph) {
        self.catList = catList
        self.firstGlyph = firstGlyph
        self.descr = descr
        _selectedGlyph = State<Glyph>(initialValue: selectedGlyph)
    }

更新:还有一个。如果不是复制粘贴错字,则您错过了NavigationLink

中的')'
NavigationLink(destination:
    SelectGlyph(catList: gard.category,
                firstGlyph: gard.ex1,
                descr: gard.title,
                selectedGlyph: Glyph() )) // << here !!
{