swiftui_SwiftUI中状态属性的内部工作

swiftuiHey! 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 prop...
摘要由CSDN通过智能技术生成

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
Hmm … ‘self’ is immutable?
嗯……“自我”是一成不变的?

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

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
Add `@State` before the var … it works!
在var之前添加`@ State`…有效!

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

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属性可以做两件事:

  1. Fix an error: “Cannot assign to property: self is immutable.”

    修正错误:“无法分配给属性: self是不可变的。”

  2. 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

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标记。” 好的,让我们返回教程并阅读说明:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值