|
In day 58, Paul uses an underscore in front of the fetchRequest variable in the initializer. I'm really not understanding the whole purpose of the underscore. Why is this code valid:
but this isn't?
gives error: Cannot assign value of type 'Int' to type 'State<Int>' |
|
The underscore refers to the property wrapper itself.
|
|
Thanks. Though I'm not sure I'm any less confused. How do you know when you can use the underscore? |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Mostly when a new property wrapper is being created, as in your first example. A new In your second example, you would need to do:
|
|
I'm having difficulty with this concept too. I've read a lot about property wrappers and they all say the same thing @roosterboy says, but I still don't understand it. What does 'a wrapped variable' actually mean? How is it different from a non-wrapped variable? How is 'refers to the value inside the property wrapper' different from 'refers the property wrapper' I understand how to use property wrappers (@State, @Binding, @StateObject, @ObservedObject, @Published) but I don't understand what's going on. I'm lost. |
|
First of all here are lists (Page 1 Page 2 ) of the terms - read at your leisure - they outline the numerous property wrapper types, with brief descriptions. You can think of property wrappers as containers - containers that have been designed for specific purposes, from varying materials (methods), with particular attributes. Typically they have more complex behaviours than say an This line says that we need a container (wrapper) that will hold some type of results.
These lines are saying how to make the container (wrapper), so we need to refer to the container itself, not the contents.
In the second example it is nonsense, because you are saying the
So the correct version is saying that the
|
|
|
|
The best way I've been able to wrap my head around property wrappers is to understand what the compiler synthesized in your code (source: SE-0258). E.G. When you use:
the compiler turns that into:
And when you use:
the compiler synthesizes:
So to understand exactly what variables and types you'll end up with in your code, you often have to peek at the documentation for the property wrapper you're using, specifically its Here's a pseudo-code template for the above compiler synthesis:
turns into:
or more simply:
So the big takeaways when using a property wrapper are:
|
|
I kinda understand the functuanality _variable does, but why we can't just asign fetchRequest to new FetchRequest without "_"? (in this code):
|
|
Because assigning a value to Assigning to |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.
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.