SwiftUI初始值设定项的圆形度 [英] SwiftUI initializer apparent circularity
问题描述
TL:DR
在此视图中似乎存在鸡与蛋的悖论
初始化程序。
代码
这种情况经常发生对我来说,我必须缺少有关 @State
的一些基本知识。即编译器似乎在说要设置 pickerIndex
之前,我不能设置 pickerIndex
,而我不能退出初始化程序,而不设置 pickerIndex
。
This happens often enough to me that I must be missing something fundamental about @State
. Namely the compiler seems to be saying I can't set pickerIndex
until pickerIndex
has been set, and I can't exit the initializer without setting pickerIndex
.
( LetterSelection
变量跟踪字母,但是我认为它的详细信息并不重要。)
(The LetterSelection
variable tracks a letter of the alphabet, but I don't think its details are relevant.)
推荐答案
注意:最好将代码作为代码提供,这样可以更容易编辑
在上述情况下,它不需要初始化属性值,而是初始化状态包装器本身,例如
In the described case it needs to initialize not property value but the state wrapper itself, like
self._pickerIndex = State(initialValue: sel.current.idx)
这篇关于SwiftUI初始值设定项的圆形度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!