Hi s710, I would check out this video for some help. It's what I'm using to move forward on this problem:
https://developer.apple.com/wwdc22/10007
Post
Replies
Boosts
Views
Activity
Hi Cyrille,
I just ran across this post, and if you haven't figured it out yet (or anyone else who finds this) the reason it's not working like you want is that @State cannot be used very well on complex data types. @State doesn't know which part of the value is changing. Anyway, the best way to overcome situations like this is to encapsulate your complex struct into a class and then call it as a @StateObject instead.
See the following:
class MyDistance: ObservableObject {
@Published var distance: Measurement<UnitLength>
init() {
distance = Measurement<UnitLength>(value: 13.37, unit: .meters)
}
}
struct ContentView: View {
@StateObject var distance = MyDistance()
var body: some View {
VStack {
Text("\(distance.distance.formatted(.measurement(width: .abbreviated, usage: .asProvided, numberFormatStyle: .number)))")
Button("Convert to cm") { print("Convert to cm");
distance.distance = distance.distance.converted(to: .centimeters)
}
Button("Convert to m") { print("Convert to m");
distance.distance = distance.distance.converted(to: .meters)
}
Button("Convert to km") { print("Convert to km");
distance.distance = distance.distance.converted(to: .kilometers)
}
}
.frame(minWidth: 300)
.padding()
}
}
I hope you find this useful.
This seems to be related to something in iOS 16.2. Try switching to a simulator running iOS 16.0; the error will not be there. Not sure what there is to do about it. In my case, it doesn't seem to impact the usability of the app I am developing.
Can confirm. I have started experiencing this issue upon upgrading to iOS 16.2.
@Informaticatropoli : Why does this work? maxWidth makes a perfect vertical scrolling view? Makes no sense...