将 ForEach 与字符串数组一起使用 - [String] 没有成员 'identified'

Using ForEach with a string array - [String] has no member 'identified'

我正在使用 Xcode 11 beta 5,但我拥有的版本不再有效。这是我的代码:

struct ModeView : View {
  @EnvironmentObject var state: IntentionState

  var body: some View {
      Picker(selection: $state.selection, label: Text("")) {
        ForEach(state.modes.identified(by: \.self)) { mode in
          Text(mode)
        }
      }.pickerStyle(SegmentedPickerStyle())
  }
}

错误在行 ForEach(uistate.modes.identified(by: \.self)) { mode in 中,上面写着:

Value of type '[String]' has no member 'identified'

当我使用 Xcode 11 beta 4 时,它运行良好。现在的问题是如何在 Xcode beta 5

中将 ForEach 与数组字符串一起使用

根据 apple 发行说明,这是他们的已知问题。我们必须等待另一个版本。

https://developer.apple.com/documentation/ios_ipados_release_notes/ios_ipados_13_beta_5_release_notes

ForEach 语法在 Beta 5 中发生了一点变化。

你试过了吗:

ForEach(state.modes, id: \.self) { mode in
    Text(mode)
}

identified(by:) 方法已被弃用,现在正确的语法是:

init(Data, id: KeyPath<Data.Element, ID>, content: (Data.Element) -> Content)

或者在将内容移动到闭包之后:

ForEach(state.modes, id: \.self) { mode in Text(mode) }

假设您有一个这样的名称数组:

let names = ["mike","Jack","jill"]

ForEach(names, id: \.self) { Text([=10=]) }

Text([=11=]) - 这将打印您的名称数组中的所有元素。

注:

Use backslashsign.self instead .self, somehow backslash sign is not working here