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

SOLVED: Correct use of SwiftData in a Picker

Forums > SwiftUI

I have a relationship between two classes: 1) Card 2) EventType

Ever Card must have a EventType, and an EventType can be used by zero or more cards. When creating a new Card in SwiftUI, I want to present a picker of all the EventTypes. I have a @State variable to hold the selected EventType? that I will then use when creating the new Card. However, my Picker is giving me an error when it runs. Picker: the selection "nil" is invalid and does not have an associated tag, this will give undefined results. I've tried multiple things for the tags, and I still see this error. Here's the Picker code.

@Query(sort: \EventType.eventName) private var events: [EventType]
@State private var selectedEvent: EventType?

init(recipient: Recipient) {
        self.recipient = recipient
        self._selectedEvent = State(initialValue: events.first)
}

var body: some View {
// removed other content of view for breviety

                        Picker(selection: $selectedEvent, label: Text("")) {
                            ForEach(events) { event in
                                Text(event.eventName)
                                    .tag(event as EventType)
                            }
                        }
}

Is there something obvious here that I am missing?

2      

Ok, FIgued it out, given that the selectedEvent is Optional, I needed to change my .tag to also be Optional.. The code is now like this

                        Picker(selection: $selectedEvent, label: Text("")) {
                            ForEach(events) { event in
                                Text(event.eventName)
                                    .tag(Optional(event))
                            }
                        }

5      

Paul, First, thanks for your site! It's great!

I to have a picker that I want to load from SwiftData. My issue is I need to initialize 'Picker( selection' to avoid the error: Picker: the selection "" is invalid and does not have an associated tag, this will give undefined results.

I want to set 'selection' to the first element in the table.

I tried this code: @State var selectedCategory:String = "" @Query(sort:\ToolCategoriesEnt.catName) var categories: [ToolCategoriesEnt] init(

    selectedCategory: String = State(initialValue: categories.first?.catName), 

This gives two errors: Cannot use instance member 'categories' as a default parameter Default argument value of type 'State<String?>' cannot be converted to type 'String'

I also tried: Picker(selection: $selectedCategory, content: { ForEach(categories) { categorie in Text(categorie.catName) .tag(categorie.catName)

                       }
                   }, label: {} ).onAppear(perform: { selectedCategory = categories.first?.catName ?? "nil"  })

This gives me an empty string not the first element.

Any suggestions would help!

Thanks for all you do!

   

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.

Click to save your free spot now

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

Reply to this topic…

You need to create an account or log in to reply.

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.