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

Escaping closure captures mutating 'self' parameter (SWIFT 5)

I'm trying to make a thread-safe struct using the @propertyWrapper, but I have such an error from the playground when I'm setting a value. The error goes, only if I change the async to sync, but I just need to have async…
Andrii Vynnyk
  • 59
  • 1
  • 3
0
votes
1 answer

When the @Published publishes the value it wraps?

I've got the publisher @Published var feedData = Feed() And this piece of code, which listens to it // some View .onReceive(feed.$feedData) { feedData in if feedData.personalTasks.count > 0 { …
0
votes
2 answers

Custom property wrapper not accurately reflecting the state of my TextField in SwiftUI, any idea why?

I've created a property wrapper that I want to insert some logic into and the "set" value is doing the right thing, but the textfield isn't updating with all uppercase text. Shouldn't the text field be showing all uppercase text or am I…
SN81
  • 323
  • 3
  • 13
0
votes
1 answer

Using @Binding with type casting in swiftui

My data model: class MyData: ObservableObject { @Published var aabbData = [AabbData]() } struct AbData: AabbData{ var x = 0 } protocol AabbData{ } In my view code, i want to downcast aabbData's element to AbData type, and bind its x…
user14105742
0
votes
1 answer

Exposing dictionary in Swift property wrappers

I have an internal dictionary that I don't want to expose to the user. Instead, I expose only certain values using properties, like this: public var artist: String? { get { return items["artist"] } set { items["artist"] = newValue …
Tobi Schweiger
  • 111
  • 1
  • 6
0
votes
2 answers

Unexpected behaviour binding SwiftUI views to Realm Object

I've developing the UI for my app which uses Realm for the backend. Text is entered by the user in a customised TextField that uses a "floating" label to make efficient use of the screen. The label animation (backed by very simple logic) is…
rustproofFish
  • 931
  • 10
  • 32
0
votes
2 answers

What's the main difference between property observers and property wrappers?

What's the main difference between property observers and property wrappers? They seem to be very similar in that they manage how the properties are stored. The only thing I can think of is that you can reuse property wrappers since there is a…
Kevvv
  • 3,655
  • 10
  • 44
  • 90
0
votes
1 answer

Decode to PropertyWrapper from JSON

I want to decode the JSON string into People as below. The age is number(Int) type, and the below code get error: "Expected to decode Dictionary but found a number instead." I think it means the @Age was treated as Dictionary
William Hu
  • 15,423
  • 11
  • 100
  • 121
0
votes
1 answer

SwiftUI View (apparently) laid out before init runs

TL;DR It seems that the ContentView below evaluates the body's if statement before init has run. Is there a race condition, or is my mental model out-of-order? Kudos A shout-out to Asperi, who provided the state-initializer equivalent that solves…
Andrew Duncan
  • 3,553
  • 4
  • 28
  • 55
0
votes
1 answer

Apply property wrapper in superclass owned property

I have created my own property wrapper for the theming of UI components like UIView, UILabel etc. class MyUIViewController: UIViewController { @Theme private override var view: UIView! // it doesnt work!!! @Theme private var myCustomView:…
justicepenny
  • 2,004
  • 2
  • 24
  • 48
0
votes
2 answers

SwiftUI code assigns one object data to all other objects in array?

I'm trying to a build a notecard app, and currently I'm working on screen where the user can enter notecards. Everything works fine, except when I type in my term and definition for one notecard, it updates all other notecards so that they have the…
0
votes
2 answers

How do you write a Swift completion block that can only be called once?

Let's say I have a Swift class that stores a completion block, and does a few asynchronous tasks. I want that block to be called by whichever of the tasks finishes first, but only that one - I don't want it to be called again when the second task…
bkbeachlabs
  • 2,121
  • 1
  • 22
  • 33
0
votes
1 answer

How to change @State property wrapper from nested view

I'm wondering how I should change @State property wrapper showErrorAlert in view below struct SettingsView: View { @State private var shouldPresent = false @State var showErrorAlert = false var body: some View { VStack { Form { …
Dawy
  • 770
  • 6
  • 23
0
votes
1 answer

Compile error on Property Wrapper in Swift 5.1

I'm figuring out Property Wrappers in Swift, but I seem to miss something. This is how I wrote a property wrapper for a dependency injection framework we use: @propertyWrapper struct Inject { var _value: Value var wrappedValue: Value…
NielsKoole
  • 296
  • 4
  • 11
0
votes
1 answer

Property Wrapped Value if accessed from other files give non optional type

struct HarkAppPreference { @UserPreferenceField(key: .userSelectedTags) static var userSelectedTags: [MLQTag] @UserPreferenceField(key: .userLastActivePlalist) static var userLastActivePlaylist: [PlayableEntity] …
the monk
  • 389
  • 4
  • 14
1 2 3
8
9