Home > Software engineering >  Swift: Initializer 'init(_:rowContent:)' requires that 'HealthStat' conform to &
Swift: Initializer 'init(_:rowContent:)' requires that 'HealthStat' conform to &

Time:03-29

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!

CodePudding user response:

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.

  • Related