Swift: Initializer 'init(_:rowContent:)' requires that 'HealthStat' conform to 'Identifiable'

1.2k views Asked by At

I'm receiving an error from Xcode that my List:

List(viewModel.stats) { stat in
            VStack(alignment: .leading) {
            Text(viewModel.value(from: stat.stat).desc)
            Text(stat.date, style: .date).opacity(0.5)
        }

Is not conforming to Identifiable:

Initializer 'init(_:rowContent:)' requires that 'HealthStat' conform to 'Identifiable'

I looked at my HealthStat File

import Foundation
import HealthKit


struct HealthStat {
    let id = UUID()
    let stat: HKQuantity?
    let date: Date
}

I have the stat declared, so I'm not sure why it's giving out to me. I'm fairly new to Swift so it's probably something I overlooked.

Any help would be greatly appreciated!

2

There are 2 answers

0
matt On BEST ANSWER

Change

struct HealthStat

To

struct HealthStat: Identifiable 

Protocol adoption is not some accidental quality that accrues implicitly because a type happens to match the protocol requirements. It's all about explicit declaration of the type. If you don't say your type adopts a protocol, it doesn't.

0
Brian Trzupek On

IF you don't want to explicitly make it Identifiable, you can also use this pattern:

List(viewModel.stats, id: \.self.id ) { stat in ....

This should work as well.