Home > Back-end >  Protocol 'Any' as a type cannot conform to 'Hashable'
Protocol 'Any' as a type cannot conform to 'Hashable'

Time:09-29

I want to iterate trough my State @State var items: [[Any]] = [], It contains arrays that each contain an Object and a Date. I get the error of Protocol 'Any' as a type cannot conform to 'Hashable'

ForEach(self.items, id: \.self) { item in                                  
BoughtImageItem(item: item[0] as! FeedPost, unlockDate: item[1] as! Date)
}

FeedPost looks like this:

struct FeedPost: Identifiable, Codable, Hashable {
    @DocumentID var id: String?
    var timestamp: Date
    var description: String
    var locked: Bool?
    var likesCount: Int
    var price: Int
    var commentsCount: Int?
    var unlockLimit: Int?
    var unlockCount: Int?
    var images: [String]?
}

CodePudding user response:

Don't use [Any], use an actual object with properties for the post and date:

struct Item: Hashable {
    let post: FeedPost
    let date: Date
}

ForEach(self.items, id: \.self) { item in                                  
    BoughtImageItem(item: item.post, unlockDate: item.date)
}
  • Related