SwiftUI provides several property wrappers that can be used for programming. Here is a list of some of them:
- @State: This property wrapper is used to manage small amounts of value type data locally to a view. It owns its data⁵.
// Example
struct ContentView: View {
@State private var name = ""
var body: some View {
TextField("Enter your name", text: $name)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
}
}
- @Binding: This property wrapper refers to value type data owned by a different view. Changing the binding locally changes the remote data too. It does not own its data⁵.
// Example
struct ChildView: View {
@Binding var name: String
var body: some View {
TextField("Enter your name", text: $name)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
}
}
struct ParentView: View {
@State private var name = ""
var body: some View {
ChildView(name: $name)
}
}
- @Environment: This property wrapper lets us read data from the system, such as color scheme, accessibility options, and trait collections. You can add your own keys here if you want. It does not own its data⁵.
// Example
struct ContentView: View {
@Environment(\.colorScheme) var colorScheme
var body: some View {
Text("Hello World")
.foregroundColor(colorScheme == .dark ? Color.white : Color.black)
.padding()
}
}
- @EnvironmentObject: This property wrapper reads a shared object that we placed into the environment. It does not own its data⁵.
// Example
class UserSettings: ObservableObject {
@Published var score = 0
}
struct ContentView: View {
@EnvironmentObject var settings: UserSettings
var body: some View {
VStack{
Text("Score : \(settings.score)")
.font(.largeTitle)
.padding()
Button(action:{
settings.score += 1
}){
Text("Increase Score")
.font(.title)
.foregroundColor(Color.white)
.padding()
.background(Color.blue)
.cornerRadius(10)
}
}.padding()
}
}
- @ObservedObject: This property wrapper refers to an instance of an external class that conforms to the ObservableObject protocol. It does not own its data⁵.
// Example
class UserSettings: ObservableObject {
@Published var score = 0
}
struct ContentView : View {
@ObservedObject var settings = UserSettings()
var body : some View{
VStack{
Text("Score : \(settings.score)")
.font(.largeTitle)
.padding()
Button(action:{
self.settings.score += 1
}){
Text("Increase Score")
.font(.title)
.foregroundColor(Color.white)
.padding()
.background(Color.blue)
.cornerRadius(10)
}
}.padding()
}
}
Source: Conversation with Bing, 3/22/2023(1) All SwiftUI property wrappers explained and compared. https://www.hackingwithswift.com/quick-start/swiftui/all-swiftui-property-wrappers-explained-and-compared Accessed 3/22/2023.
(2) swift - How to use Attributed String in SwiftUI - Stack Overflow. https://stackoverflow.com/questions/59531122/how-to-use-attributed-string-in-swiftui Accessed 3/22/2023.
(3) Introducing SwiftUI | Apple Developer Documentation. https://developer.apple.com/tutorials/swiftui/ Accessed 3/22/2023.
(4) Attributes | Documentation - Swift.org. https://docs.swift.org/swift-book/documentation/the-swift-programming-language/attributes/ Accessed 3/22/2023.
(5) swift - What does the SwiftUI @State
keyword do? - Stack Overflow. https://stackoverflow.com/questions/56438730/what-does-the-swiftui-state-keyword-do Accessed 3/22/2023.
(6) SwiftUI Property Wrappers | Kodeco - raywenderlich.com. https://www.kodeco.com/21522453-swiftui-property-wrappers Accessed 3/22/2023.
(7) What are SwiftUI’s property wrappers and how are they used?. https://siempay.medium.com/what-are-swiftuis-property-wrappers-and-how-are-they-used-9b848ae0ba0d Accessed 3/22/2023.