Picker the selection "" is invalid and does not have an associated tag, this will give undefined results

After upgrading to macOS 13, Xcode started to print to console

Picker: the selection "" is invalid and does not have an associated tag, this will give undefined results.

what is wrong here?

struct Piiker: View {

    @State var selectOption: [String] = ["1","2","3"]

    @State var fieldValue: String = ""

    var body: some View {

        Picker("Select number", selection: $fieldValue){

            ForEach(selectOption, id: \.self){

                Text($0)

            }

        }

    }

}
Answered by Rauli in 734238022

I solved it finally so.

struct Piiker: View {

    @State var selectOption: [String] = ["1","2","3"]

    @State var fieldValue: String = "" 

    var body: some View {

        Picker("Select number", selection: $fieldValue){

            Text("").tag("") //basically added empty tag and it solve the case

            ForEach(selectOption, id: \.self){

                Text($0)

            }

        }

    }

}

But it still odd, that Picker need that initial value tag for Dynamic dataset. Previously Picker was able to handle ForEach cases the tags, by itself.

Either work with optional String either initialise selection with a default value

You have provided to the Picker the selectOption array as its available selection values. You have set the initial selection value to be an empty string which is not in the selectOption array.

You need to specify an initial value that is in the array for the console message to go away.

@State var fieldValue: String = "1" // any of the values in selectOption



You can also set the selection binding to be an optional string, but then you will get this console message:

Picker: the selection "nil" is invalid and does not have an associated tag, this will give undefined results.

Accepted Answer

I solved it finally so.

struct Piiker: View {

    @State var selectOption: [String] = ["1","2","3"]

    @State var fieldValue: String = "" 

    var body: some View {

        Picker("Select number", selection: $fieldValue){

            Text("").tag("") //basically added empty tag and it solve the case

            ForEach(selectOption, id: \.self){

                Text($0)

            }

        }

    }

}

But it still odd, that Picker need that initial value tag for Dynamic dataset. Previously Picker was able to handle ForEach cases the tags, by itself.

A possible approach is to set the "selection" value to an actual option in the initializer. This avoids the need to add a tag for an empty selection.

struct PickerTest: View {
    @State private var selectOption: [String] = ["1","2","3"]
    @State private var fieldValue: String = ""

    init() {
        self._fieldValue = State(initialValue: selectOption.first ?? "")
    }

    var body: some View {
        Picker("Select number", selection: $fieldValue){
            ForEach(selectOption, id: \.self){
                Text($0)
            }
        }
    }
}

Picker the selection "" is invalid and does not have an associated tag, this will give undefined results
 
 
Q