|
Hi, For my practice I'm working a small project and have a @State variable that needs to be a @Binding var. When I change the variable I get some errors that I don't know how to fix. I searched and tried some different things, but with no luck. Can anyone point me in the right direction? @State variable
Complete view
|
|
Computed PropertiesWhat is a computed property? You may design your application with variables that change based on user's input. For example, what's the difficulty setting for a game your user is about to play? How many levels in the game? Is the game over yet? Other variables aren't based on a user's choice, instead they are calculated. For example:
body is a Computed PropertyNow look closely at your It takes a box of parts, perhaps some variables and it has a calculation with ONE purpose. That is, at the end of its calculations, it will produce a The However, you must do this OUTSIDE the body's calculation. This is a concept taught in the earliest lessons in the 100 Days of SwiftUI. Are you following the 100 Days of SwiftUI course? or are you guessing at SwiftUI syntax? Keep CodingMaybe this lesson will help? See -> Custom Component |
|
This is not taught in early 100 Days of SwiftUI lessons. Why do you have vertical stacks with only one item in them?
|
|
@Obelix, Thank you for your reply. Your answer makes sense, but when I place it outside the bodies view I have other problems, so I started trying things. I will look into your feedback and how I can fix this. As for your questions: I am working on the 100 days of swiftui, currently at day 72. But I noticed I was starting to forget the things I've learned so I started to work on something of my own to implement and refresh the knowledge. The first VStack with one line is just a remainder of some testing I did. It's temporary and will be removed. The second VStack is just a temporarly horizontal line |
|
Now we're getting somewhere...
Keep Coding!Show us your revised code and show us your errors. More imporantly! Tell us what you're attempting to achieve and let us know what you think your code is doing! We can give you tips, hints, and additional lessons. Some of us will give you the direct answer! (Personally, I prefer you try and learn. So i seldom give a direct answer! Sorry!!) |
|
@Obelix, I'm having trouble fixing my issue. I moved the @State var outside the body (to the property initializer?) and changed it to a @Binding var. Now I get the error "Cannot use instance member 'baseDate' within property initializer" and "Cannot convert value of type 'Date' to specified type 'Binding<Date>'". I tried to make the binding a Property wrapper, but now I get the error "Property wrapper cannot be applied to a computed property" which tells me I cannot do this with a @Binding. I also tried to move the content of the var in a method buat that's also no allowed. The reason I want "dateToDisplay" to be a binding is because I place this view (NavView) inside another view, and there I want to filter an array with the value from "dateToDisplay". Inside the NavView you can select another date with a slider that changes the result from the array. Hope this makes sense :-) I'm not looking for the straight up answer, I want to learn, but I'm having trouble to find the right direction to search for the solution. Hope you can help me out again, thanks in advance. |
|
I solved the issue.
The value now gets updated on drag end. Good enough for now. |
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.