TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Selecting a field from a model

Forums > SwiftUI

Below is a small snippet of code. I am new to swift and just do not understand how to retrieve the value fromthe picker.

No value is returned....

Any help would be greatly appreciated. Thank!

@Query(sort: \roleModel.role) var roles: [roleModel]

@State var selectedRole = ""

HStack { Text("Role") Picker(" ", selection: $selectedRole) { ForEach (roles, id: .self) {role in Text(role.role) } } Text("You selected: (selectedRole)") }

3      

Oddly enough, I changed my code to you suggestion and after I selected an element from the picker, it placed a check mark by it.

But then the picker went blank and Text("You selected: (selectedRole)") displayed blanks.

Idealy, based upon the section I need to pull some values and update several fields on the view.

I tried .onChange and .onSubmit but neither would allow me to display the selected picker value.

Sorry, i'm really lost here, but I really appreciate your reply.

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your 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.