TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: SwiftData enum No exact matches in call to instance method 'setValue' error

Forums > SwiftUI

I'm getting an "No exact matches in call to instance method 'setValue'" error for a property that has a enum as a value. How do I fix this? Any help would be appericated.

enum FluidUnit: CaseIterable, Identifiable {
  case ounce, liter
  var id: Self { self }

  var title: String {
    switch self {
    case .ounce:
      return "ounce"
    case .liter:
      return "liters"
    }
  }
}
@Model
class Drink: Identifiable, Hashable {

  let id: UUID = UUID()
  let name: String = ""
  var shortName: String = ""
  var amount: Double = 0.0
  let unitOfMeasure: FluidUnit = FluidUnit.ounce
  let date: Date = Date()
  var image: String = "water"
  var favorite: Bool = false

  init(name: String, amount: Double, unitOfMeasure: FluidUnit, image: String, favorite: Bool = false, shortName: String = "") {
    self.id = UUID()
    self.name = name
    self.amount = amount
    self.unitOfMeasure = unitOfMeasure
    self.date = Date()
    self.image = image
    self.favorite = favorite
    self.shortName = shortName
  }

  static func == (lhs: Drink, rhs: Drink) -> Bool {
    lhs.id == rhs.id
  }

  func hash(into hasher: inout Hasher) {
      hasher.combine(id)
  }
}

   

Try adding Codable conformance to the FluidUnit enum definition. (I don’t think you need Identifiable.)

1      

Thanks for this. I added Codable earlier today and it didn't work. I cleaned the build folder and added it again and it worked!

   

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.