HomeiOS Developmentios - SwiftUI View Inside A ForEach Has Mistaken ID From OnTapGesture

ios – SwiftUI View Inside A ForEach Has Mistaken ID From OnTapGesture


I’m doing a ForEach and creating views and including an on faucet gesture

// objects = [MyObject]
                    VStack {
                        ForEach(objects, id: .id) { obj in
                            MyView(imageURL: obj.imageURL)// <=== That is the RIGHT url for every object
                            .onTapGesture {
                                didTap(obj.objectId) // <== That is the WRONG id for every object
                            }
                        }
                    }

The id is a string based mostly on some properties that make it distinctive

struct MyObject: Codable, Identifiable {
    var id: String {
        get { return uniquePropertyString }
    }

    var imageURL: String
    var objectId: String

The on faucet gesture is returning the ID of an object 2 indexes deeper within the array. I assumed by having a singular unchanging id property this would not occur but it surely nonetheless is.

Are you able to counsel why the onTap gesture is fallacious on this ForEach?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments