Since I don't know what the rest of your SymbolView
looks like, I used this to test your encoding code:
struct SymbolView: View {
var imageName: String = ""
var code: String = ""
@State var position:CGPoint = CGPoint(x:200, y:200)
var body: some View {
VStack(spacing: 20) {
Text("imageName: \(imageName)")
Text("code: \(code)")
Text("position: {\(position.x), \(position.y)}")
Button("Move position!") {
position = CGPoint(x: 3000, y: 10)
}
Button("Encode!") {
do {
let encodedView = try JSONEncoder().encode(self)
print(String(data: encodedView, encoding: .utf8))
} catch {
print(error)
}
}
}
}
}
And it worked just fine. If I encoded without changing position
, I got 200, 200
. If I changed position
and then encoded, I got 3000, 10
.
Perhaps you aren't really changing position
before you encode it? Or maybe something else is going on in the code you didn't post?
I would, however, recommend not directly encoding your SymbolView
. Pull the data into a type you can (en|de)code separately and use as an @State
value in the View
.