|
Hi there, newbie here :-) first: Thank you Paul Hudson for all your content and thank you to all the other contributing members of this forum! After viewing a lot of Swift-tutorials I still feel like I don't know anything about coding. Again I am stuck at a point I think I should be able to solve it by myself, but... I want a Picker to show my saved Entity's names. It workes in a List, but I can't get the Picker to work. All examples I can find are using Arrays, not fetched Data, so I think I should maybe create an Array. Can somebody please help me out? Does the Binding var need to be inside the Struct/View? Thanks a lot, Freddy
|
|
hi, does the hope that helps, DMG |
|
hi, thank you for your suggestion. Indeed I had commented-out the NavigationView, but unfotunately this doesn't change anything. greetings |
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Try
This code |
|
Mhm... still throws error "Value of type 'NSManagedObject' has no member 'name'". The error disappears, when I force it But there is an remaining error "Missing argument for parameter 'newUserIndex' in call" at the line I put the struct in the body-View
|
|
But I don't see which value you want to assign to your newUserIndex. |
|
I see your point... this remains from the example, where the source is an Array of Strings. The Binding var |
|
You can use the tag modifier
But you will need a https://www.ioscreator.com/tutorials/swiftui-picker-tutorial |
|
Hooray, it finally works! the Picker contains the workers names now. In fact, I don't even have implemented a class yet, only my Worker-Entity in .xcdatamodeld (I think I have to add creating of classes to my ToDo). I had an id-attribute, specified as UUID. I added an Int-attribute to test, but it seems to work with my UUID-attribute as well. Also I changed the name of newUserIndex-State var to the more logic var That is what its looks like now:
Now I've reached the next problem. I need to figure out how to read out the pickers selection.
In an example they are using the State var in a |
|
|
|
tried it with |
|
|
|
I suggest watching this great video from Stanford CS193P, Spring 2020, lecture 11. This goes in depth into Pickers, and it's around 31:00 where the pickers come into play. Then watch lecture 12 where the instructor changes everything to use Core Data. Helped me tremendously. Most importantly, what ever type you use as a $selection, is what should be the result of the picker. Lecture 11: https://www.youtube.com/watch?v=fCfC6m7XUew&feature=youtu.be Lecture 12: https://www.youtube.com/watch?v=yOhyOpXvaec&feature=youtu.be CS193p course lectures and code can be found at the link below and they are a great supplement to HWS site. https://cs193p.sites.stanford.edu |
|
I struggled with getting Core Data directly bound to a picker as well. The code below is a standard Xcode Core Data template project modified to work with a picker. It also automatically changes the picker to a new item when a new item is created. I like this solution because the type of the selection is the same as the type of the things being selected.
|
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.