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

Is Property Wrapper @Lazy variable thread safe?

We now have a new way to make a lazy variable. It is described in swift-evolution/proposals/0258-property-wrappers.md: @propertyWrapper enum Lazy { case uninitialized(() -> Value) case initialized(Value) init(wrappedValue:…
6
votes
2 answers

How to receive @Published var in main thread

I keep the Model as a Published var in the ViewModel and Observe it from the View. When the model process goes into a background thread, if you publish the model value, the Xcode thread checker will react. Publishing changes from background threads…
user38155
  • 61
  • 3
5
votes
2 answers

How to test and mock property wrappers in Swift?

Let's say I have a very common use case for a property wrapper using UserDefaults. @propertyWrapper struct DefaultsStorage { private let key: String private let storage: UserDefaults var wrappedValue: Value? { get { …
gasho
  • 1,926
  • 1
  • 16
  • 20
4
votes
3 answers

Using iOS 15+ API (@AccessibilityFocusState) without dropping support for earlier iOS versions

Apple introduced the @FocusState and @AccessibilityFocusState and their respective APIs for iOS 15. Typically when I have an app that supports multiple versions and I need to use a new API, I would wrap the code with if #available (iOS x) {} or use…
c_booth
  • 2,185
  • 1
  • 13
  • 22
4
votes
1 answer

When I change selection of the SwiftUI List with mouse click didSet of the @Published property is called twice

I use SwiftUI for my Mac OS app. When I change selection (from Inbox to Today) of the List in SidebarView with mouse click didSet is called twice(Sidebar body is calculated twice too). If I change selection with arrow (down | up) didSet is called…
4
votes
2 answers

Passing binding to a variable of type property wrapper - losing underlying type

I'm trying to pass a binding to a variable that is created with a property wrapper. It appears that I lose access to the underlying type, when I pass the binding to another view. In the following example code, I demonstrate that I can update the…
Philip Pegden
  • 1,732
  • 1
  • 14
  • 33
4
votes
1 answer

In SwiftUI, the application freeze without any warning when slide back halfway but released before completion

The following code reproduced the error: import SwiftUI struct ContentView: View { @State private var number: Int = 5 var body: some View { NavigationView() { VStack(spacing: 20) { …
Boom PT
  • 374
  • 4
  • 11
4
votes
1 answer

Property Wrapper for CurrentValueSubject - memory management

I would like to create a property wrapper for CurrentValueSubject. I have done this like that: @propertyWrapper public class CurrentValue { public var wrappedValue: Value { get { projectedValue.value } set {…
Damian Dudycz
  • 2,622
  • 19
  • 38
4
votes
1 answer

Swift properyWrapper cannot convert value of declared type to value of specified type

Here is my property wrapper: @propertyWrapper struct UserDefaultsBacked { let key: String let storage: UserDefaults = .standard var defaultValue: Value var wrappedValue: Value? { get { let value =…
Maysam
  • 7,246
  • 13
  • 68
  • 106
4
votes
2 answers

Custom Property Wrapper that Updates View Swift

Xcode 11.3, Swift 5.1.3 I am trying currently to create a custom property wrapper that allows me to link variables to a Firebase database. When doing this, to make it update the view, I at first tried to use the @ObservedObject @Bar var foo = [].…
3
votes
1 answer

@AppStorage inside ObservableObject - How is `objectWillChange` triggered from inside the property wrapper?

In iOS 14.5, Apple made a change to @AppStorage so that it can be properly used within an ObservableObject: AppStorage property wrappers now work as expected when contained inside an ObservableObject, causing the system to emit the objectWillChange…
SwiftedMind
  • 3,701
  • 3
  • 29
  • 63
3
votes
1 answer

How to avoid a retain cycle on my custom dependency injection tool?

I have created a custom propertyWrapper to inject my dependencies in code so when testing the code, I can pass a mock in place using the WritableKeyPath link to the object in memory. This is how I would use it in production code. It is very…
Roland Lariotte
  • 2,606
  • 1
  • 16
  • 40
3
votes
2 answers

Why does my SwiftUI view not get onChange updates from a @Binding member of a @StateObject?

Given the setup I've outlined below, I'm trying to determine why ChildView's .onChange(of: _) is not receiving updates. import SwiftUI struct SomeItem: Equatable { var doubleValue: Double } struct ParentView: View { @State private var…
pinglock
  • 982
  • 2
  • 12
  • 30
3
votes
1 answer

SwiftUI Passing variables in view hierarchy

I have a question regarding the architecture of my Swift / SwiftUI app consisting of a ListView, detail views and the detail views hold 2 input views. Specifically I want to know if this is the right way to do it. The architecture is as follows: The…
jjuser19jj
  • 1,637
  • 3
  • 20
  • 38
3
votes
2 answers

Initialize optional @AppStorage property with non-nil value

I need an optional @AppStorage String property (for a NavigationLink selection, which required optional), so I declared @AppStorage("navItemSelected") var navItemSelected: String? I need it to start with a default value that's non-nil, so I…
Steve M
  • 9,296
  • 11
  • 49
  • 98
1
2
3
8 9