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
2 answers

SwiftUI: How to update published property of one class (ObservableObject) with published property of a location manager class?

I want to update the Published property of my ChallengeManager class with data passed in from LocationManager. Here is the simplified code with the relevant bits: LocationManager final class LocationManager: NSObject, ObservableObject { var…
Hyoryusha
  • 175
  • 1
  • 11
1
vote
0 answers

Property type 'MyViewModel' does not match that of the 'wrappedValue' property of its wrapper type

I have a property wrapper to avoid dealing with implicitly unwrapped optionals. It works great except when I try to use a custom wrapper to handle weak properties: @propertyWrapper public struct WeakMaybeUninitialized { …
user15102768
1
vote
1 answer

Alternative to implicitly unwrapped optional using @propertyWrapper in swift

I found myself using a lot of implicitly unwrapped optionals when initialiser injection would not work or when creating mvvm modules for examples: class TodoView: UIViewController { var viewModel: TodoViewModelProtocol! } Not only it…
user15102768
1
vote
0 answers

Initialization doesn't call the override class function for property wrapper

I have a base class and a derived class... the base class has a class function userDefaultKeyPrefix() which indicates the user defaults key for user default storage using a wrapper however the AdvancedManager class ends up with the BaseManager class…
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
1
vote
1 answer

Automatic decodable synthesis for decodable property wrappers

Let's say I have decodable property wrapper: @propertyWrapper struct OptionalDecodable: Decodable { var wrappedValue: Value? } The compiler does synthesize init for the following struct Model: Decodable { @OptionalDecodable…
Ihar
  • 13
  • 3
1
vote
1 answer

@State autogenerated underscore-prefixed member variable

I noticed via an Xcode autocompletion suggestion that @State seems to not only autogenerate a $-prefixed member for accessing the corresponding Binding (as is commonly known), but also a _-prefixed member, seemingly exposing the actual State…
Milo Wielondek
  • 4,164
  • 3
  • 33
  • 45
1
vote
0 answers

How can I update the Observable Object from an ARView extension in SwiftUI?

I am trying to Update the globalDataTransfer class from the ARView extension and reflect the changes on to ArView. Below is the globalDataTransfer function class globalDataTransfer: ObservableObject{ @Published var val: String = "No…
1
vote
1 answer

How to Make Custom Property Wrapper Bindable, Possibly with @State?

I have created a custom property wrapper that writes and reads from a file as its setter and getter. @propertyWrapper struct Specifier { let key: String let defaultValue: Value let plistPath: String var prefs:…
YulkyTulky
  • 886
  • 1
  • 6
  • 20
1
vote
1 answer

How to integrate enums in propertyWrapped UserDefault

The struct below will work for primitive data types such as Int, String, Double, etc., Is there a way to make this work for enums so that I won't have to use rawValues an manual parsing? @propertyWrapper struct Storage { let objectName: String …
Zonily Jame
  • 5,053
  • 3
  • 30
  • 56
1
vote
1 answer

@AppStorage property wrapper not able to be toggled with VoiceOver

I am trying to use a new @property wrapper in iOS 14 @AppStorage. It how ever doesn't seem to be able to be toggled with VoiceOver if I use it in a toggle. A normal @State private var property works fine. I can confirm that these work with VoiceOver…
1
vote
1 answer

Swift @ObservedObject initialize with another property

EDIT added the source code showing declaration of the variables I am trying to initialize a view model property which is an @ObservedObject @ObservedObject private var viewmodel : ExpenseListViewModel I want to initialize the view model with a…
Arun
  • 136
  • 6
1
vote
2 answers

Parameterized @PropertyWrapper with non-default parameters in auto-synthesized init method?

v1. No parameters: ✅ works as expected Normally, I can create a Trimmed wrapper like this: @propertyWrapper struct Trimmed { private(set) var value: String = "" var wrappedValue: String { get { value } set { value =…
Senseful
  • 86,719
  • 67
  • 308
  • 465
1
vote
0 answers

Swift @propertyWrapper 'Self' refers to the enclosing struct/class instead of property wrapper struct

I coded a simple property wrapper in Swift and I tried to use Self and self to refer to the property wrapper struct/class Reference in the projectedValue property: @propertyWrapper class Reference { private var value: T …
Louis Lac
  • 5,298
  • 1
  • 21
  • 36
1
vote
1 answer

Swift property wrappers initilaization error

I am trying to use PropertWrapper for email validation. But when i try to initialize the eamilId varaible with empty string i am getting error : Incorrect argument label in call (have 'wrappedValue:', expected 'emailId:') Here is the code of My view…
1
vote
1 answer

Implementing UserDefaults using properyWrapper causes weird problem

I am trying to implement UserDefaults class using @propertyWrapper. What I am trying to do is creating a wrapper class for my app user preferences. So I wrote following code. @propertyWrapper struct Storage { private let key: String …
atalayasa
  • 3,310
  • 25
  • 42
1 2 3
8 9