UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Using a Picker with FetchRequest

Forums > SwiftUI

I am trying to select an item from a picker populated from a FetchRequest. The Picker populates correctly but none of the items is tappable/selectable.

I wrote the view below as a test case - I thought that the code for selecting from an array would be similar to selecting from a FetchRequest.

struct GetNameView: View {
    @State private var name1 = ""
    @State private var name2 = ""

    // array for Picker 1
    private var names1 = ["fred", "barney", "dino"]

    // FetchRequest for Picker 2
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(key: "firstName", ascending: true)],
        animation: .default)
    private var names2: FetchedResults<Name>

    var body: some View {
        NavigationView {
            VStack {
                //Picker 1 => selects from array
                Picker("Select name1", selection: $name1) {
                    ForEach(names1, id: \.self) {
                        Text($0)
                    }
                }
                .pickerStyle(SegmentedPickerStyle())

                Text("name1 selected \(name1)")

                // Picker 2 => selects from FetchRequest
                Picker("Select name2", selection: $name2) {
                    ForEach(names2, id: \.self) {
                        Text("\($0.wrappedFirstName)")
                    }
                }
                .pickerStyle(SegmentedPickerStyle())

                Text("name2 selected \(name2)")
            }
        }
    }
}

Does the FetchRequest data need to be specially formatted for a Picker or is there another problem?

2      

The @State property that you use for the selection of a Picker has to match the type of the tag assigned to the values of the Picker. By default, the tag is whatever type is used to generate the Picker's data.

That's why name1 (a String) works with names1 (an array of Strings). But name2 (a String) doesn't work with names2 (a FetchedResults<Name>).

You can get around this by explicitly assigning a tag to each item:

Picker("Select name2", selection: $name2) {
    ForEach(names2, id: \.self) {
        Text("\($0.wrappedFirstName)").tag($0.wrappedFirstName) 
        //assuming wrappedFirstName is a String
        //note also: if wrappedFirstName is a String, then you don't need 
        //to use string interpolation in a Text
    }
}
.pickerStyle(SegmentedPickerStyle())

2      

@roosterboy, again, thank you!

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.