Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

enter image description here

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)
  }
}
like image 825
syclonefx Avatar asked Jul 15 '26 22:07

syclonefx


1 Answers

I figured it out. I needed FluidUnit to conform to Codable. I also need to clean the build folder and the error went away.

like image 72
syclonefx Avatar answered Jul 17 '26 15:07

syclonefx