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-wrapper
133
questions
-1
votes
1
answer
92
views
How to force non-optional type (for array element) on compile time in Swift? [closed]
struct MyStruct {
@ArrayOfNonOptionalElements var arr: [SomeNonOptionalType]
}
where @ArrayOfNonOptionalElements is a propertyWrapper.
So, how to force non-optionality for the type inside? Is it ...
0
votes
0
answers
32
views
Apple's own Invalidating property wrapper example doesn't compile
In iOS 15, Apple introduced the @Invalidating property wrapper. The documentation shows an example:
class MyView: UIView {
@Invalidating(.display) var badgeColor: UIColor
@Invalidating(....
0
votes
3
answers
100
views
What is the difference between (@StateObject & @StateObject) and (@StateObject & @ObservedObject) in Parent and Child Views
@StateObject & @StateObject in Both Parent and ChildView
import SwiftUI
class Counter: ObservableObject {
@Published var count: Int = 0
func increment() {
count += 1
}
}
...
-2
votes
1
answer
78
views
Binding with a ternary between parent/child views in SwiftUI [closed]
Consider this code in which I'm trying to continuously animate a set of circles from red to blue:
struct ContentView: View {
let timer = Timer.publish(every: 1.0, on: .main, in: .common)....
2
votes
2
answers
161
views
Swift Property Wrapper on Struct
I am struggling to create a swift struct, where I can use a property wrapper to decode some JSON that sometimes requires a value to conform to a specific type, but other times may become a dictionary ...
1
vote
0
answers
65
views
SwiftUI Alert setting presentation condition true in it's button action closure not showing the alert again
So the thing I want to do is that I have an api call if it fails I show an alert with message, OK button and Retry Button.
Everything is working good but when I do retry it don't shows the alert again ...
4
votes
1
answer
879
views
Can't use Property Wrappers on a class marked with @Observable
I have a class which is similar to:
final class Person {
@Resettable var name = "John Doe"
func revertName() {
name = $name
}
}
@propertyWrapper
struct Resettable&...
0
votes
1
answer
213
views
Swift: Union Type in Property Wrapper
In Swift, you can specify a union type using an enum. For example:
enum IntOrString {
case int(Int)
case string(String)
}
Now I want to write a property wrapper, which I can apply to ...
0
votes
1
answer
110
views
SwiftUI - immutable @State variable when change it "from outside" via UIHostController
Situation
During migration of some UIKit view classes to SwiftUI, I have faced next case:
I have a simple view class, responsible for data fetching and displaying. Its structure is pretty simple and ...
-1
votes
1
answer
100
views
How to have @State that is an array with the size that depends on a FetchRequest
I have an app that is outlined in the following. Most of the code I have adapted from https://www.hackingwithswift.com/books/ios-swiftui/how-to-combine-core-data-and-swiftui
But the part I have ...
1
vote
2
answers
185
views
Are Swift Property Wrappers attributes?
Using a propertyWrapperseems to have the same syntax as attribute. For instance, compare this custom propertyWrapper:
@propertyWrapper
struct Capitalize {
private var value: String = ""
...
0
votes
0
answers
17
views
Initialise Property Wrapper in the init [duplicate]
I have the following simple property wrapper
@propertyWrapper
struct TestWrapper<Value> {
private var value: Value
init(wrappedValue: @autoclosure @escaping () -> Value) {
...
1
vote
1
answer
209
views
Custom property wrapper in a SwiftUI viewmodel
I've been working on a SwiftUI wrapper around a MKMapView, and in trying to work on a binding value for the visible area of the map, I created a custom property wrapper that allows the ...
0
votes
1
answer
119
views
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 ...
1
vote
3
answers
182
views
Adding additional attribute to @State property
I have a SwiftUI view that takes user name in a String declared as a State property.
How can I add an additional attribute to the same variable?
struct Login: View {
@Trimmed(characterSet: ....