swiftui
Hey! Just to clarify, this article is about the @State
property (unique to SwiftUI) and not the general concept of state. They’re related, but state itself is just an idea while the @State
property is an actual, visible, and tangible object. It’s also very versatile, almost like magic …
嘿! 只是为了澄清,本文是关于@State
属性(对于SwiftUI而言是唯一的),而不是有关state的一般概念 。 它们是相关的,但是状态本身只是一个想法,而@State
属性是实际的,可见的和有形的对象。 它也非常通用,几乎就像魔术一样……
背景 (Background)
If you’ve been working with SwiftUI, chances are that you once made a property and tried to change its value, only to get this weird error: “Cannot assign to property: ‘self’ is immutable.”
如果您一直在使用SwiftUI,那么您曾经有机会创建一个属性并尝试更改其值 ,只会得到这个奇怪的错误:“无法分配给属性:'self'是不可变的。”
![Image for post](https://miro.medium.com/max/9999/1*WKNbzfjH2oJ5-9hfyza8og.png)
Above, pressedButton
is a var
, so its value is mutable (changeable) — but the error message says that “self” is immutable!
上面, pressedButton
是一个var
,因此它的值是可变的(可变的),但是错误消息指出“ self”是不可变的!
![Image for post](https://miro.medium.com/max/9999/1*Af_GlxHwuo0AqTndLniMUw.png)
So pressedButton
is mutable, but self
isn’t? Let’s search up the error message.
因此, pressedButton
是可变的,但self
不是吗? 让我们搜索错误消息。
Hacking With Swift tells us that “If you want to change a property’s value while your program runs, you should mark it using @State
.” Ok, let’s see if it works …
Hacking with Swift告诉我们 :“如果要在程序运行时更改属性的值,则应使用@State
标记。” 好吧,让我们看看它是否有效……
![Image for post](https://miro.medium.com/max/9999/1*4MDZSzOqZKOHbqttOTcM7A.png)
That was an easy fix! But how did it get rid of the “self
is immutable” error — did it make self
mutable?
那很容易解决! 但是,它如何摆脱“ self
是不可变的”错误-它使self
变得可变了吗?
The answer turned out to be way more fascinating (and complicated *sigh*) than I had expected. Even only last week, I had thought @State
properties were exclusively used for linking with UI elements — like this:
答案竟然比我预期的更吸引人(和复杂的*叹气*)。 甚至仅在上周,我还认为@State
属性仅用于与UI元素链接-像这样:
Here, the rounded rectangle accesses @State var rectangleIsGreen
and automatically updates whenever that property’s value is changed:
在这里,圆角矩形访问@State var rectangleIsGreen
并在属性值更改时自动更新:
![Image for post](https://miro.medium.com/freeze/max/9999/1*cGNUktpRu-hO3spz0oWVDQ.gif)
So @State
properties are used to sync with the UI … but how does that have anything to do with fixing the “self
is immutable” error? Is @State
some sort of all-purpose magic powder? Not really …
因此, @State
属性用于与UI同步……但是,与解决“ self
是不可变的”错误有什么关系呢? @State
是某种通用的魔术粉吗? 并不是的 …
研究 (Research)
It looks like @State
properties can do two things:
看起来@State
属性可以做两件事:
Fix an error: “Cannot assign to property:
self
is immutable.”修正错误:“无法分配给属性:
self
是不可变的。”- Sync with the UI and update it automatically. 与UI同步并自动更新。
This is getting confusing, so let’s do some research!
这变得令人困惑,所以让我们做一些研究吧!
能力#1 | 修复错误:“无法分配给属性:'自身'是不可变的” (Ability #1 | Fix an error: “Cannot assign to property: ‘self’ is immutable”)
![Image for post](https://miro.medium.com/max/9999/1*5ZGk1tRKnS0DGl9HlEhoWg.png)
Remember, Hacking With Swift said that “if you want to change a property’s value while your program runs, you should mark it using State
.” OK, let’s go back to the tutorial and read the explanation:
请记住,Hacking With Swift曾说过:“如果要在程序运行时更改属性的值,则应使用State
标记。” 好的,让我们返回教程并阅读说明: