Ios 在ForEach循环SwiftUI中使用索引获取索引
我是SwiftUI新手,我正在尝试获取以下ForEach循环的索引Ios 在ForEach循环SwiftUI中使用索引获取索引,ios,swift,iphone,mobile,swiftui,Ios,Swift,Iphone,Mobile,Swiftui,我是SwiftUI新手,我正在尝试获取以下ForEach循环的索引 ForEach(self.postViewModel.posts, id: \.postId) { post in HStack(alignment: .top, spacing: 25) {
ForEach(self.postViewModel.posts, id: \.postId) { post in
HStack(alignment: .top, spacing: 25) {
Header(post : post)
Footer(post: post)
我已尝试使用以下索引:
ForEach(self.postViewModel.posts.indices, id: \.postId) { post in
但我犯了这个错误
Value of type 'Range<Array<Post>.Index>.Element' (aka 'Int') has no member 'postId'
在
索引
的变体中,应使用self
作为id
ForEach(self.postViewModel.posts.indices, id: \.self) { post in
二者兼备的可能变体
ForEach(Array(self.postViewModel.posts.enumerated()),
id: \.1.postId) { (index, post) in
ForEach(Array(self.postViewModel.posts.enumerated()),
id: \.1.postId) { (index, post) in