Learn Courses My Dashboard

I'm trying to make a ToDo list app but I get this error

    func updateTask(id: ObjectId, completed: Bool) {
        if let localRealm = localRealm {
            do {
                 let taskToUpdate = localRealm.objects(Task.self).filter(NSPredicate(format: "id == %Q", id))
                 guard !taskToUpdate.isEmpty else { return }
                
                 try localRealm.write {
                     taskToUpdate[0].completed = completed
                     getTasks ()
                     print("Updated task with id \(id)! Completed status: \(completed) ")
                 }
              } catch {
                 print("Error updating task \(id) to Realm: \(error)")
            }
        }
    }
    

For that piece of code for my app I get an error saying this "Value of type ‘Task’ has no member ‘completed’

How can I fix this?

Welcome to the community!!

I can see it in your code, but next time also mention you’re using Realm

Your model doesn’t have a property called “completed” that’s the error, what’s your model look like?

import Foundation
import RealmSwift

class Task: Object, ObjectKeyIdentifiable {
    
    @Persisted(primaryKey: true) var id: ObjectId
    @Persisted var title = ""
    @Persisted var completedStatus =  false
    
}

There is my model and next time I’ll mention I’m using Realm.

The taskToUpdate[0].completed Is different than your model completedStatus

Oh I never even noticed that I fixed it thank you!

1 Like