1

First SwiftUI project :) I Have a NavLink from a list in a top level view that calls a detail view. it was working until I needed to add a @State var of a Struct into the detail view, now the call parameter list does not match what the compiler expects:

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

and the detail view starts off:

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 {

and the Glyph Struct is defined as:

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
    }
}

The error is in the top level ContentView navLink Destination:

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

I have tried several variations in the call list and tried to remove the @State wrapper in SelectGlyph, all to no effect. Any help would be appreciated! It would be nice if I knew how to specify a local variable in a View without it being a required argument in the call (Im sure Im missing something here!)

In response to Asperi, I tried the explicit initializer for the SelectGlyph view and still got the same error in the same place:

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)

with the SelectGlyph view now modified to have an explicit init(). I tried specifying selectedGlyph in the init() as _selectedGlyph and as 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 Answer 1

1

It is here

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

you need explicit initializer here, like

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)
    }

Update: One more. If it is not copy-paste typo then you missed on ')' in NavigationLink

NavigationLink(destination:
    SelectGlyph(catList: gard.category,
                firstGlyph: gard.ex1,
                descr: gard.title,
                selectedGlyph: Glyph() )) // << here !!
{
Sign up to request clarification or add additional context in comments.

2 Comments

Sadly the suggestion to explicitly init() the SelectGlyph view Struct did not work. Maybe I am missing something in my edits?
Ha ! the missing paren was balanced by an extra at the bottom of the Struct! Many thanks! May the Covid pass you over!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.