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
0
votes
0 answers

Can't get an array of objects to update the view

I have a fairly simple piece of code that I am trying to write containing a displayed array and a draggable object inside that array. The problem though is that it compiles and displays the array, but does not update the view when I tap and drag the…
0
votes
0 answers

Issue in Setting Value to @State variable in SwiftUI

I'm trying to set value to a @State variable alertText once a button click action is performed. However it isn't working and value in alertText isn't getting set. I'm new to SwiftUI so I think I'm missing something obvious. I would appreciate any…
Jaffer Sheriff
  • 1,444
  • 13
  • 33
0
votes
0 answers

Swinject always return nil when trying to resolve any object

I have the following Swinject implementation in my app - import Foundation import Swinject @propertyWrapper struct Inject { let wrappedValue: Component init() { self.wrappedValue = Resolver.shared.resolve(Component.self) …
Alon Shlider
  • 1,187
  • 1
  • 16
  • 46
0
votes
0 answers

How can I force ProgressView to run its task again?

I have content that is loaded in the background. Loading is a long running task and I want to show data as soon as it's available. My view show loaded content and a ProgressView whenever there is more content to be expected. struct MyListView :…
user965972
  • 2,489
  • 2
  • 23
  • 39
0
votes
1 answer

How to read property from a struct when being in @ObservableObject

I don't know how to read a property that is in a struct from a class that is an Observable Object. Context: I'm trying to build an app which consists of 2 views: a custom calendar; a popup with a header 'Daily Joke', date formatted as 'MM-dd-yyyy'…
Gleb
  • 31
  • 7
0
votes
1 answer

SwiftUI Child Views not updating as expected with environmentObject

Please see my example provided, I have recreated my pattern as accurately as possible while leaving out details that are not relevant to the question. I have @Published property variables in my viewmodel which are updated/assigned after a fetch to…
Andre
  • 562
  • 2
  • 7
  • 18
0
votes
1 answer

SwiftUI: Observable Object does not update in View?

I am struggling here for days now: I have a async function that get's called onRecieve from a timer in a LoadingView. It calls the getData function from the class ViewModel. Data gets fetched with an HTTP Get Request and compared: if the fetched ID…
0
votes
1 answer

Swift PropertyWrapper. Can't initialize variable

So, i try to create a property wrapper that strips a phone number from unwanted characters and add a country code to it: @propertyWrapper struct MSISDN { private var _wrappedValue: String public var wrappedValue: String { get { return…
Victor Benetatos
  • 396
  • 3
  • 12
0
votes
1 answer

Property Wrapper doesn't affect TextField

I wrote MaxCount propertyWrapper to limit String count in TextField. However, while Text view shows trimmed String, TextField shows full String. I can achieve expected behavior via below ViewModifier, but this doesn't seem a good practice to me, I…
OguzYuksel
  • 88
  • 6
0
votes
1 answer

Property Wrappers vs Mirroring

Happy holidays! I have a property wrapper that is used to define the key in a key-value pair. @propertyWrapper final public class KeyValuePair { public var wrappedValue: T? private(set) var key: String public init(key: String) { …
crazyname
  • 135
  • 1
  • 15
0
votes
1 answer

@propertywrapper in SwiftUI

I created a @propertyWrapper to limit the number a variable can reach. I tried it in a SwiftUI view with a button that increases the value of the variable and it works, the variable stops at the maximum number set in the initializer. However if I…
Jad
  • 66
  • 6
0
votes
1 answer

How to have a dependency set as a @propertyWrapper be a @ObservableObject?

I have a first @property wrapper that is made to be UserDefaults called @UserDefault(key: .isSignedIn, defaultValue: false) var isSignedIn: Bool. it is working fine as a publisher by using it this way, $isSignedIn to update my SwiftUI view when used…
Roland Lariotte
  • 2,606
  • 1
  • 16
  • 40
0
votes
1 answer

Environment property wrapper throws +entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name

Why is the entityForName on Environment(\.managedObjectContext).wrappedValue always nil? I get this error +entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name 'Project' With…
Daniel
  • 50
  • 1
  • 7
0
votes
0 answers

How to use .focusedValue in a SwiftUI list

I've adapted an example from blog post which lets me share data associated with the selected element in a ForEach with another view on the screen. It sets up the FocusedValueKey conformance: struct FocusedNoteValue: FocusedValueKey { typealias…
c_booth
  • 2,185
  • 1
  • 13
  • 22
0
votes
1 answer

How to abstract singletons using a Property Wrapper on types?

In a protocol, I'd like to create a single instance from functions so I use a container to store the static instances like this: protocol MyProtocol { func networkService() -> NetworkService } extension MyProtocol { func networkService()…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
1 2 3
8 9