Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 413 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does the underscore mean before a variable in Swiftui in an init()?

#1
What does the underscore mean before momentDate? Why is it needed?

[![enter image description here][1]][1]


[1]:
Reply

#2
The underscored variable name refers to the underlying storage for the [`Binding`](

[To see links please register here]

) struct. This is part of a language feature called [Property Wrappers](

[To see links please register here]

).

Given one variable declaration, `@Binding var momentDate: Date`, you can access three variables:

- `self._momentDate` is the `Binding<Date>` struct itself.
- `self.momentDate`, equivalent to `self._momentDate.wrappedValue`, is a `Date`. You would use this when rendering the date in the view's body.
- `self.$momentDate`, equivalent to `self._momentDate.projectedValue`, is also the `Binding<Date>`. You would pass this down to child views if they need to be able to change the date.

For `Binding`, the "projected value" (`$`) is just `self`, and the difference between `_` and `$` is only in the [access level](

[To see links please register here]

). However, other property wrappers may project a different type of value (see the `@SmallNumber` example in the [language guide](

[To see links please register here]

)).
Reply

#3
[Official Docs](

[To see links please register here]

)

A Binding is one of various Property Wrappers.

When you use a `@PropertyWrapper` on a `var`, it synthesizes convenience code.

That includes automatically creating a private var named `_yourPropVar`, whose type is `YourPropertyWrapper` (e.g. `Binding`).

When you prefix that with underscore, you're referencing the synthesized private variable - the Property Wrapper itself.

When you reference `yourPropVar` instead, without the `underscore`, it's accessing the synthesized wrapped value getter instead.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through