|
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? |
|
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 |
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.