Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在ForEach循环SwiftUI中使用索引获取索引_Ios_Swift_Iphone_Mobile_Swiftui - Fatal编程技术网

Ios 在ForEach循环SwiftUI中使用索引获取索引

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) {

我是SwiftUI新手,我正在尝试获取以下ForEach循环的索引

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