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
2
votes
1 answer

Swift - How to access @Published var from func outside of view?

I'm trying to remove the logic from the view, while keeping the benefits of SwiftUI. Idea 1 works but it makes use of an extra variable than I would want to. Idea 2 gives error: Property wrappers are not yet supported on local properties. The view…
habibiboss
  • 321
  • 4
  • 7
2
votes
1 answer

Simultaneous accesses when using PropertyWrapper

I want to create a property wrapper, that would store a callback block, and execute it every time, when it's value changes. Something like simple KVO. It works fine, but there is one problem with it. If I use the property itself in this callback…
Damian Dudycz
  • 2,622
  • 19
  • 38
1
vote
0 answers

Custom property wrapper in a SwiftUI viewmodel

I've been working on a SwiftUI wrapper around a MKMapView, and in trying to work on a binding value for the visible area of the map, I created a custom property wrapper that allows the MKMapViewDelegate to send out the current visible region, while…
RL2000
  • 913
  • 10
  • 20
1
vote
3 answers

Adding additional attribute to @State property

I have a SwiftUI view that takes user name in a String declared as a State property. How can I add an additional attribute to the same variable? struct Login: View { @Trimmed(characterSet: .whitespaces) @State private var userName: String =…
Kaunteya
  • 3,107
  • 1
  • 35
  • 66
1
vote
0 answers

How to mutate the binding property inside the closure?

I have an array of values. I want to change it inside the closure. In this closure, I will iterate through the array and initialize the Picker with the binding to every element. Using this code, I can mutate the values in the array with the help of…
Vitya Shurapov
  • 2,200
  • 2
  • 27
  • 32
1
vote
1 answer

Generic type protocol Published property

I got x data models (struct) that conform to a protocol. I want to have in my viewModel a @Pubished property that represents one of those type and access each property to assign in the UI, Why is this approach not possible? I got the compiler is…
David.C
  • 257
  • 3
  • 17
1
vote
1 answer

Is there a way to find Swift properties wrapped in property wrappers using reflection?

I've been trying to get an @ObservedObject-like property wrapper (let's call it @Observed) working that would listen to Combine events published by @Published properties of a class adhering to the ObservableObject protocol. I want to do it this way…
mjKosmic
  • 63
  • 5
1
vote
1 answer

How to make `ObservableObject` with `@Published` properties `Codable`?

My codable observable Thing compiles: class Thing: Codable, ObservableObject { var feature: String } Wrapping feature in @Published though doesn’t: class Thing: Codable, ObservableObject { @Published var feature: String } Class 'Thing'…
1
vote
2 answers

How to build a Swift object that can control the mutability of its stored properties

I would like to create a set of objects that exhibit the following behavior: Each has a BOOL property -- call it dataLocked -- that is initially false. Each has a set of stored properties whose values may be set, but not read, whenever dataLocked…
Greg Anderson
  • 540
  • 4
  • 14
1
vote
0 answers

Using @FocusState variables in NavigationLink View makes this view return back instantly to the previous view

I have SignUpView divided across two smaller views. Both of them connected in NavigationView of the first one. NavigationLink in the first view leads to the second one. I tried using iOS 15 @FocusState property wrappers to variables and .focused…
Vader20FF
  • 271
  • 2
  • 9
1
vote
0 answers

Swift property wrappers on function arguments and initializers

I try to use new @propertyWrappers on arguments defined in type initializer like in the below code: init(@Inject services: TodoServiceContainer? = nil) { service = services?.todoService } But Xcode doesn't compile such code and throws such…
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
1
vote
1 answer

How can I use @propertyWrapper for Decodable with optional keys?

I'm using a property wrapper to decode the strings "true" and "false" as Booleans. I also want to make the key optional. So if the key is missing from the JSON, it should be decoded as nil. Unfortunately, adding the property wrapper breaks this and…
Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
1
vote
2 answers

Access @StateObject from function outside of view

I have a class (WatchlistClass) that conforms to the "ObservableObject" protocol and that holds a @Published var (watchlist). The var, in turn, holds a whole array of stocks and some information ([StockInformation]), which is supposed to update my…
1
vote
1 answer

Dynamic key property wrapper

I have a simple property wrapper written to easily get and set user preferences in the UserDefaults. @propertyWrapper struct UserDefault { let key: String let defaultValue: Value var wrappedValue: Value { get {…
Bram
  • 2,718
  • 1
  • 22
  • 43
1
vote
1 answer

How do I create optional parameters for Published property wrappers while using MVVM architecture?

In my pursuit to learn SwiftUI, I am really struggling with this MVVM architecture. With that being said, I have a project which I have been working on to learn this. With some help of other folks I have gotten this far. The current issue is, I need…
hallux
  • 61
  • 1
  • 8
1 2 3
8 9