GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

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.