SwiftUI initializer apparent circularity

☆樱花仙子☆ 提交于 2020-05-28 11:58:09

问题


TL:DR

There seems to be a chicken-and-egg paradox in this View initializer.

The Code

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.

(The LetterSelection variable tracks a letter of the alphabet, but I don't think its details are relevant.)


回答1:


Note: it is better to provide code "as code", so it could be easier edited

In the described case it needs to initialize not property value but the state wrapper itself, like

self._pickerIndex = State(initialValue: sel.current.idx)


来源:https://stackoverflow.com/questions/61650040/swiftui-initializer-apparent-circularity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!