Code in xcode 6 no longer working in xcode 6.3.1

168 views Asked by At

before this to delete a cell value in UItableview i use the following code and it works as it should .... but now it give me an error. THE ERROR: Cannot invoke 'deletedObjects' with an argument list of type '(NSManagedObject)' on the following line:

context.deletedObjects(results[indexPath.row] as NSManagedObject)

How to fix this? The codes in the function involved:

     let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default")
            var appDel = (UIApplication.sharedApplication().delegate as! AppDelegate)
            var context = appDel.managedObjectContext
            var request = NSFetchRequest(entityName: "UserCholesterol")
            request.returnsObjectsAsFaults = false
var results: NSArray = context!.executeFetchRequest(request, error: nil)!
        context.deletedObjects(results[indexPath.row] as NSManagedObject)

        context!.save(nil)
        totalEntries = totalEntries - 1
        tblLog.reloadData()
2

There are 2 answers

0
Bannings On BEST ANSWER

Deleting a managed object is done with context.deleteObject(anObject). Try this:

context.deleteObject(results[indexPath.row] as NSManagedObject)
1
tounaobun On

deletedObjects is just a property of NSManagedObjectContext,not a method.But there is a method called - deleteObject:.

Try to replace to this line of code:

context.deleteObject(results[indexPath.row] as! NSManagedObject) // in Xcode 6.3 and above,you have to add ! after `as` keyword.