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