What Does the Dollar Sign Do in Swift/Swiftui

What does the dollar sign do in Swift / SwiftUI?

The $ is used in conjunction with property delegates.

It's not an operator, but a prefix (thanks @matt!).

For more about property delegates, see this Swift Evolution document.

e.g. in @State var aState = false, State is a property delegate.

This means that if we write:

  • aState we're accessing a Bool value
  • $aState we're accessing a Binding<Bool> value

Different property delegates will generate different values.

@State variable $ sign usage

@State is a property wrapper struct that just wraps any value to make sure your view will refresh or redraw whenever that value changes.
(https://developer.apple.com/documentation/swiftui/state)

Using a $ dollar sign provides access to the projectedValue of the state struct, which in this case gives a Binding (https://developer.apple.com/documentation/swiftui/binding), best explained by the documentation:

Use a binding to create a two-way connection between a view and its
underlying model. For example, you can create a binding between a
Toggle and a Bool property of a State. Interacting with the toggle
control changes the value of the Bool, and mutating the value of the
Bool causes the toggle to update its presented state.

I want to add $ sign to TextField in SwiftUI

currency formatter is the way to go, but if you just want to show a $ in the TextField as you type, you could use something like this:
(you can of course combine this approach with a Formatter)

struct ContentView: View {
@State var price = ""

var body: some View {
VStack {
ZStack(alignment: .leading) {
if price.isEmpty {
Text("Enter total budget")
}
HStack {
TextField("", text: Binding(
get: { price },
set: { newVal in
if price.starts(with: "$") {
price = newVal
} else {
price = "$" + newVal
}
})).keyboardType(.decimalPad)
}
}.padding(20)
Text("number entered: " + String(price.dropFirst()))
}
}
}

How to position plus sign right in the middle of circle

use Image(systemName: "plus").foregroundColor(.white) instead of Text("+")

In text "+" symbol doesn't have to be in the middle of the view because of text layout.

SFSymbols are more convenient in this regard. Also you can specify size with .font(.system(size: 10))

What does the underscore mean before a variable in Swiftui in an init()?

The underscored variable name refers to the underlying storage for the Binding struct. This is part of a language feature called Property Wrappers.

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. However, other property wrappers may project a different type of value (see the @SmallNumber example in the language guide).

What does $0 and $1 mean in Swift Closures?

$0 is the first parameter passed into the closure. $1 is the second parameter, etc. That closure you showed is shorthand for:

let sortedNumbers = numbers.sort { (firstObject, secondObject) in 
return firstObject > secondObject
}

SwiftUI: Property initializers run before 'self' is available in @AppStorage use

You can't refer to $currency when you define cur. This is a limitation of Swift. It obvious to you that currency has a value already, but Swift won't let you use it until after the initializer is done setting up self. This has nothing to do with AppStorage -- it's a rule that property initializers cannot refer to other (non-static) properties.

So, this

@State private var currency = "$"
@AppStorage("cur") var cur = "\($currency)"

Could be

@State private var currency = "$"
@AppStorage("cur") var cur = "$" // set it to the same value

or

private static let dollar = "$"

@State private var currency = SettingsView.dollar
@AppStorage("cur") var cur = SettingsView.dollar

Does anyone know what this stack is called SwfitUI?

This is a picker. to be precise, this is a segmented picker.

You can create it like so:

struct ContentView: View {
@State private var favoriteColor = 0

var body: some View {
Picker("What is your favorite color?", selection: $favoriteColor) {
Text("Red").tag(0)
Text("Green").tag(1)
Text("Blue").tag(2)
}
.pickerStyle(.segmented)
}
}

When we create the picker we pass in a binding (when we change the picker's value it will know to switch to it) this is the thing with the dollar sign ($)

The next thing is to add the segments.
So we add text views with a tag attached to each one.

Lastly we need to set the picker style (in this case the segmented)

.pickerStyle(.segmented)

I suggest you to look here: Create a segmented control and read values from it

Hope this helps!



Related Topics



Leave a reply



Submit