Questions tagged [property-wrapper]

Property Wrappers are a feature of Swift 5.1 and beyond. Should be tagged onto questions using `@` to mark a Property Wrapper. Should not be used for `@` referring to Objective-C interoperability (e.g. @IBAction).

Property Wrappers are a feature of Swift 5.1 and beyond. Should be tagged onto questions using @ to mark a Property Wrapper. Should not be used for @ referring to Objective-C interoperability (e.g. @IBAction).

Property wrappers can be defined by using struct, class, or enum. It can also used when declaring properties within those types.

Uses:

  • Constraining Values
  • Transforming Values on Property Assignment
  • Changing Synthesized Equality and Comparison Semantics
  • Auditing Property Access

References


Related tags

123 questions
66
votes
2 answers

What does the dollar sign do in Swift / SwiftUI?

This tutorial by Apple about SwiftUI uses a dollar sign to bind data, and I‘m having trouble finding more information about this data binding in SwiftUI. Toggle(isOn: $showFavoritesOnly) { You use the $ prefix to access a binding to a state…
vrwim
  • 13,020
  • 13
  • 63
  • 118
27
votes
5 answers

SwiftUI: ObservableObject does not persist its State over being redrawn

Problem In Order to achieve a clean look and feel of the App's code, I create ViewModels for every View that contains logic. A normal ViewModel looks a bit like this: class SomeViewModel: ObservableObject { @Published var state = 1 //…
KonDeichmann
  • 878
  • 1
  • 8
  • 21
25
votes
1 answer

What is the difference between @EnvironmentObject and @ObservedObject?

I have been reading about the property wrappers in SwiftUI and I see that they do a great job, but one thing which I really don't get is the difference between @EnvironmentObject and @ObservedObject. From what I learned so far, I see that…
Dakata
  • 1,227
  • 2
  • 14
  • 33
20
votes
3 answers

How to compose swift property wrappers?

I have recently been experimenting with swift property wrappers and wondered if there was any way of composing them together in order to achieve a more modular architecture. E.g: @WrapperOne @WrapperTwo var foo: T Looking through the documentation…
Ben
  • 388
  • 3
  • 15
20
votes
1 answer

Access name of object being wrapped, in Swift property wrapper implementation

I'm using Swift property wrappers to define something like: @MyWrapper var foo: Int And in the implementation of the property wrapper, I'd like to access the name of the variable, foo, as a string. Something like this: @propertyWrapper public…
Chris Prince
  • 7,288
  • 2
  • 48
  • 66
19
votes
4 answers

Is it correct to expect internal updates of a SwiftUI DynamicProperty property wrapper to trigger a view update?

I'm attempting to create a custom property wrapper supported by SwiftUI, meaning that changes to the corresponding properties values would cause an update to the SwiftUI view. Here is a simplified version of what I have: @propertyWrapper public…
Pop Flamingo
  • 3,023
  • 2
  • 26
  • 64
15
votes
2 answers

Optional @ObservableObject in SwiftUI

I want to have an optional @ObservedObject in SwiftUI but I keep getting a compile time error of. Property type 'AModel?' does not match that of the 'wrappedValue' property of its wrapper type 'ObservedObject' Here is some minimum reproducible…
Jon Vogel
  • 5,244
  • 1
  • 39
  • 54
10
votes
1 answer

Difference between @Binding and Binding in Swift

I understand that @Binding is a property wrapper and I believe that Binding is a form of type casting but what is the difference in practical terms? For example declaring a var like: @Binding var hidden: Bool versus var hidden: Binding
pinglock
  • 982
  • 2
  • 12
  • 30
10
votes
1 answer

How to implement a custom property wrapper which would publish the changes for SwiftUI to re-render it's view

Trying to implement a custom property wrapper which would also publish its changes the same way @Publish does. E.g. allow my SwiftUI to receive changes on my property using my custom wrapper. The working code I have: import…
Pavel Lobodinský
  • 1,028
  • 1
  • 12
  • 25
9
votes
1 answer

Atomic property wrapper only works when declared as class, not struct

I have created a "lock" in Swift and an Atomic property wrapper that uses that lock, for my Swift classes as Swift lacks ObjC's atomic property attribute. When I run my tests with thread sanitizer enabled, It always captures a data race on a…
9
votes
4 answers

Add @Published behaviour for computed property

I am trying to make a ObservableObject that has properties that wrap a UserDefaults variable. In order to conform to ObservableObject, I need to wrap the properties with @Published. Unfortunately, I cannot apply that to computed properties, as I use…
Nicolas Degen
  • 1,522
  • 2
  • 15
  • 24
7
votes
1 answer

Is there a way to access enclosing instance `ObservableObject` to call `objectWillChange.send()` from anywhere in a property wrapper

I'm trying to make a property wrapper similar to Combine's Published one(for my project needs), but with ability to modify the wrapped property by sending a value to a publisher, stored in projectedValue, like this: // in class @PublishedMutable var…
7
votes
2 answers

Combining custom property wrapper with @Published

I wish to apply a custom property wrapper to a variable already wrapped in @Published, nesting them like (A) @Custom @Published var myVar or (B) @Published @Custom var myVar (notice the application order of the wrappers). In the case of (A) I get…
Milo Wielondek
  • 4,164
  • 3
  • 33
  • 45
7
votes
1 answer

UserDefault property wrapper not saving values iOS versions below iOS 13

I am using a property wrapper to save my User Default values. On iOS 13 devices, this solution works great. However on iOS 11 and iOS 12, the values are not being saved into User Defaults. I read that property wrappers are backwards compatible so I…
dayloosha
  • 123
  • 4
7
votes
1 answer

SwiftUI Can I use Binding get set custom binding with @Binding property wrapper?

How can I use Binding(get: { }, set: { }) custom binding with @Binding property on SwiftUI view. I have used this custom binding successfully with @State variable but doesn't know how to apply it to @Binding in subview initializer. I need it to…
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
1
2 3
8 9