Referencing instance method 'setValue(for:to:)' on 'Array' requires that 'Task' conform to 'PersistentModel'

I am trying to add into @Model this class:

@Model
class Test: Identifiable {
    var id: Int
    var tasks: [Task] = []
    
    init(id: Int, tasks: [Task]) {
        self.id = id
        self.tasks = tasks
    }
}

while Task is a simple struct

struct Task: Identifiable {
    var id: String
    var name: String
    
    init(name: String) {
        self.id = UUID().uuidString
        self.name = name
    }
}

And I am getting this 3 errors: Referencing instance method 'setValue(for:to:)' on 'Array' requires that 'Task' conform to 'PersistentModel' and Referencing instance method 'getValue(for:)' on 'Array' requires that 'Task' conform to 'PersistentModel' and Referencing instance method 'setValue(for:to:)' on 'Array' requires that 'Task' conform to 'PersistentModel' exactly for the line in the Test class:

var tasks: [Task] = []

which opens hidden code as following:

{
    init(newValue) accesses (_$backingData) {
                    _$backingData.setValue(for: \.tasks, to: newValue)
        }

    get {
                    _$observationRegistrar.access(self, keyPath: \.tasks)
                    return self.getValue(for: \.tasks)
        }

    set {
                    _$observationRegistrar.withMutation(of: self, keyPath: \.tasks) {
                            self.setValue(for: \.tasks, to: newValue)
                    }
        }
}

Is it not possible to have an array as part of the new @Model SwiftData approach? Is there a simple fix for it that I cannot find out?

Hello,

I have similar problem, I just want to store enum in my @Model like this:

class Cat {
    init(name: String,
         birthDay: Date?,
         breed: Breed?
    ) {
        self.name = name
        self.birthDay = birthDay
        self.breed = breed
    }
    let name: String
    let birthDay: Date?
    let breed: Breed?
    @Attribute(.unique)
    let id = UUID()
}
enum Breed: String {
    case unknown = "Unknown"
    case ragdoll = "Ragdoll"
}

I also get three errors:

Instance method 'setValue(for:to:)' requires that 'Breed' conform to 'PersistentModel'

No exact matches in call to instance method 'getValue'

No exact matches in call to instance method 'setValue'

Have not found much information how to deal with that.

Well, I have found out that if I add

Codable, Hashable

to my enum or your

struct Task: Identifiable, Codable, Hashable {...}

compiler stops complaining.

As far as I know (and it's not that far), you have to change your struct Task into a @Model class task.

(But you'll probably have the same issue that I have, an "EXC_BREAKPOINT" crash when trying to accessing it…)

I have same issue and I follow this path the compiler did't rise red alert but change to grey, but does anyone have such err

Cannot convert value of type 'PredicateExpressions.Conjunction<PredicateExpressions.Equal<PredicateExpressions.KeyPath<PredicateExpressions.KeyPath<PredicateExpressions.Variable<Transaction>, User>, UUID>, PredicateExpressions.KeyPath<PredicateExpressions.Value<User>, UUID>>, PredicateExpressions.Equal<PredicateExpressions.KeyPath<PredicateExpressions.Variable<Transaction>, Date>, PredicateExpressions.Value<Date>>>' to closure result type 'any StandardPredicateExpression<Bool>'

This issue come from the code

func showTransByDate(user: User, date: Date) -> [Transaction] {
    let predictor = #Predicate<Transaction>{$0.tranInitiator.id == user.id && $0.tranDate == date}
    fetchDescriptorTrans = FetchDescriptor(predicate: predictor)
    let transList = try? contextTransaction?.fetch(fetchDescriptorTrans)
    if(transList!.isEmpty){return []}
    else{
      return transList!
    }
Referencing instance method 'setValue(for:to:)' on 'Array' requires that 'Task' conform to 'PersistentModel'
 
 
Q