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

Passing object with wrapped properties don't allow compile on (set)

I followed this guide on creating wrapped properties with user defaults: https://www.vadimbulavin.com/advanced-guide-to-userdefaults-in-swift/ Now, I have LocalStorage class which will take this "Storage()" class at the end of the tutorial and then…
Travis
  • 21
  • 3
1
vote
1 answer

Property Wrappers: "Cannot use instance member within property initializer; property initializers run before 'self' is available"

I am trying to create the bellow property wrapper @propertyWrapper public struct ConstraintValue { private let view:UIView private let contraint:NSLayoutConstraint public init(contraint:NSLayoutConstraint,view:UIView) { …
1
vote
1 answer

Cannot access property wrapper across modules in Swift

In my Common module (framework project) have below property wrapper for user defaults, @propertyWrapper public class MyUserDefaultWrapper { let key: String let defaultValue: T public init(_ key: String, defaultValue: T) { …
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
1
vote
1 answer

Use Swift @propertyWrapper for dynamic default value?

I need a Swift property that -- if the value has not yet been set -- defaults to another value. This can be implemented using backing-store private properties. For instance, for a property num that should default to a global defaultNum, it would…
Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
0
votes
1 answer

A property wrapper causes JSONDecoder to fail when property is not in the json string

I have a data model class with custom rules to deserialize from json. Specifically, the property flexibleProp can be defined in JSON as either String? or Int?. On my model class it should always be Int?. I have implemented a propertyWrapper to…
Mando
  • 11,414
  • 17
  • 86
  • 167
0
votes
2 answers

Adding setter to @State property impossible?

If I change @State var pan: Int to this: @State var pan: Int { set(x) { self.pan = min(max((x), -50), 50) } get { return self.pan } } I get the error: Cannot apply property wrapper to computed property. The error…
user1944491
  • 559
  • 1
  • 8
  • 24
0
votes
0 answers

Swift @FirestoreQuery property wrapper and async await

I need to fetch some data from Firestore, process them, and present them to a view. The data should reflect any changes in Firestore (say I alter a value). I am pretty sure I need to attach a snapshotListener for this (getDocuments() method will not…
christostsang
  • 1,701
  • 3
  • 27
  • 46
0
votes
1 answer

How to initialize a @State variable using the value of a variable that comes from another view SWIFTUI

I need to initialize a @State variable using the value that comes from another view. Since my code is very long, I put a simplified code: struct IconoLista: View { // MARK: Variables generales @State var listaElegida : Listas =…
0
votes
0 answers

DynamicProperty Breaks PropertyWrapper Outside SwiftUI.View

Consider the following property wrapper. It keeps track of value in wrappedValue, and also caches old value in wrappedValueOld. @propertyWrapper struct OldValueCache: DynamicProperty { @State private var field: Value var wrappedValue:…
Vakho
  • 87
  • 7
0
votes
1 answer

How to access @propertyWrapper argument in Swift

Let's examing following code: @propertyWrapper struct Argument { var wrappedValue: Int? var argument: String } struct Model { @Argument var property: Int? @Argument(argument: "bar") var foo: Int? = nil } extension Model { …
Makalele
  • 7,431
  • 5
  • 54
  • 81
0
votes
1 answer

Swift: Loading and Saving with @propertyWrapper and an [String : Bool]

I have the following property wrapper to store my values: @propertyWrapper struct UserDefault { var key: String var wrappedValue: T? { get { if let data = UserDefaults.standard.object(forKey: key) as? Data { …
Nathanael Tse
  • 171
  • 13
0
votes
1 answer

@UIApplicationDelegateAdaptor(AppDelegate.self)?

When using SwiftUI's @UIApplicationDelegateAdaptor, many in the Swift community(SO, blogs, courses) register the app delegate class like this: @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate Apple's documentation uses code similar…
Marcy
  • 4,611
  • 2
  • 34
  • 52
0
votes
0 answers

Get marked variable name as default value for propertyWrapper

Lets say I have a property Wrapper to fetch Feature Flag from a service and assign to a Boolean if it is enabled of not @propertyWrapper final class FeatureToggle { var clientContainer: ConfigurableFeatureFlagClient private let key:…
Cayoda
  • 189
  • 6
0
votes
1 answer

Work with Property wrappers in nested views in SwiftUI

I'm fairly new to SwiftUI and I ran into this issue trying to figure out how to work with Environment object in many views. I'm trying to move to the next view after successful login which is happening, I'm then updating @Published Isloggedin to…
0
votes
1 answer

SwiftUI: calling objectWillChange.send() not updating child view

I have a rather complicated set of views nested in views. When I trigger a button action, I pass along an optional block through my viewModel class which calls objectWillChange.send() on that viewModel and I know that it's being triggered because…
Mercutio
  • 1,152
  • 1
  • 14
  • 33
1 2 3
8 9