如何在 SwiftUI 中使用 ForEach 数组
How to use array of ForEach in SwiftUI
我想创建一个带有 ForEach
循环的数组。
像这样:
let a = [
ForEach(b) { c in
"create the element in the array"
}
]
这是我试过的代码:
let places = [
ForEach(viewModel.posts) { post in
Place(latitude: post.location.latitude, longitude: post.location.longitude)
}
]
但是我有一个错误:
Static method 'buildBlock' requires that 'Place' conform to 'AccessibilityRotorContent'
你有什么想法吗?
正如@jnpdx所说,您可以使用Array
的map(...)
方法来实现您的目标:
let places: [Place] = viewModel.posts.map { post in
Place(latitude: post.location.latitude, longitude: post.location.longitude)
}
我想创建一个带有 ForEach
循环的数组。
像这样:
let a = [
ForEach(b) { c in
"create the element in the array"
}
]
这是我试过的代码:
let places = [
ForEach(viewModel.posts) { post in
Place(latitude: post.location.latitude, longitude: post.location.longitude)
}
]
但是我有一个错误:
Static method 'buildBlock' requires that 'Place' conform to 'AccessibilityRotorContent'
你有什么想法吗?
正如@jnpdx所说,您可以使用Array
的map(...)
方法来实现您的目标:
let places: [Place] = viewModel.posts.map { post in
Place(latitude: post.location.latitude, longitude: post.location.longitude)
}