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
3
votes
4 answers

Swift - Specify conformity to a protocol of a generic type parameter

What I'm trying to do is to have two generic type parameters where one is a specific type and the other a protocol like so: @propertyWrapper struct Implementation where T : AnyObject, T : P { // Compiler error var wrappedValue: P {…
funct7
  • 3,407
  • 2
  • 27
  • 33
3
votes
3 answers

Swift Compilation Error for Property Wrappers with Codable in multiple files

Compilation failed for property wrappers with codable in multiple files. I found test codes in Swift source below: property_wrappers_codable_multifile_other.swift @propertyWrapper struct Printed: Codable { var wrappedValue:…
lucky.li
  • 31
  • 2
2
votes
1 answer

PropertyWrapper subscript is not called. WHY?

I am implementing my own AtomicDictionary property wrapper as follows: @propertyWrapper public class AtomicDictionary: CustomDebugStringConvertible { public var wrappedValue = [Key: Value]() private let queue =…
YanivH
  • 539
  • 4
  • 18
2
votes
0 answers

Composing property wrappers in Swift with Vapor + Fluent

I've implemented a system to automatically generate Fluent's Migrations by reflecting 'registered' Models, saving the boilerplate of creating them manually. To achieve this, I must allow the specification of any DatabaseSchema.FieldConstraints…
JacobCXDev
  • 417
  • 6
  • 16
2
votes
1 answer

Swift property wrapper not compiling any more in Swift 5.4+?

The following code used to work for Swift 5.2, and possibly Swift 5.3. (Last build was November 2020) @propertyWrapper class ActionBindable
funct7
  • 3,407
  • 2
  • 27
  • 33
2
votes
1 answer

Property wrapper: change empty to Optional

I've created the following extension: import Foundation extension Collection { /// Returns `nil` if empty var nonEmptyValue: Self? { isEmpty ? nil : self } } Now I'd like to make it a property wrapper so I could use it like…
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
2
votes
1 answer

@MainActor is not safely when it used with propertyWrapper?

Combined use @mainActor and propertyWrapper is not safe? The code will execute directly in the background thread. It is a bug? Here is the demo: struct DetailView: View { @MainActor @Environment(\.dismiss) private var dismiss …
2
votes
3 answers

How to initialize the new Firebase @FirestoreQuery property wrapper with variables?

I use the new @FirestoreQuery property wrapper and it's working with the code below. But i need to use it/initialize it with variables, how can i do it (see below what I am trying to do)? import SwiftUI import FirebaseFirestoreSwift struct…
2
votes
2 answers

How to update an element of an array in an Observable Object

Sorry if my question is silly, I am a beginner to programming. I have a Navigation Link to a detail view from a List produced from my view model's array. In the detail view, I want to be able to mutate one of the tapped-on element's properties, but…
2
votes
2 answers

Is it possible to nest property wrappers in Swift when using @ObservedObject?

I have recently started to dig into the wonderful world of SwiftUI, Combine, and property wrappers and am struggling to combine @ObservedObject with the @Injected property wrapper I wrote to inject dependencies into my views. Most of the time my…
Nickersoft
  • 684
  • 1
  • 12
  • 30
2
votes
1 answer

How can I prevent SwiftUI from reinitializing my wrapped property just like it does with @StateObject?

From what I've read whenever you instantiate an object yourself in your view, you should use @StateObject instead of @ObservedObject. Because apparently if you use @ObservedObject, SwiftUI might decide in any moment to throw it away and recreate it…
Evert
  • 2,022
  • 1
  • 20
  • 29
2
votes
3 answers

Initialize @State property with another @State property value in SwiftUI

I'm trying to get progress value of a progress bar based on another value. Both of these variables (waterQuantity and progress) are in property wrappers @State This is my code : struct CircularProgressBar: View { @State var waterQuantity: Int @State…
Quentin C
  • 307
  • 3
  • 13
2
votes
2 answers

How do I use an existing property in a property wrapper when self hasn't been initialized? (SwiftUI)

I have a struct with two variables inside property wrappers. One of the variables is supposed to be computed from the other. When I try to do this, I get the following error: Cannot use instance member 'name' within property initializer; property…
Evan93
  • 155
  • 11
2
votes
2 answers

ForEach NavigationLink with State and Binding

I'm trying to generate a ForEach with a NavigationLink and use State and Binding to pass some entity around: struct ContentView: View { @Environment(\.managedObjectContext) var moc @FetchRequest( entity: MyEntity.entity(), …
gurehbgui
  • 14,236
  • 32
  • 106
  • 178
2
votes
1 answer

Debounced Property Wrapper

After spending some time creating a @Debounced property wrapper I'm not happy with the readability of the code. To understand what's going on you really need to understand how a Property wrapper works and the concept of the wrappedvalue and…
Magnus T
  • 220
  • 1
  • 9
1 2
3
8 9