标签云

微信群

扫码加入我们

WeChat QR Code

I made an app which's using core data. I made a function which saves 1 or 2 values / write data into core data. This is the following method:func saveName(name: String) {let myDate:NSDate = NSDate()let context = self.fetchedResultsController.managedObjectContextlet entity = self.fetchedResultsController.fetchRequest.entity!let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName(entity.name!, inManagedObjectContext: context) as NSManagedObjectif markCell == true {newManagedObject.setValue(name, forKey: "markedCell")markCell = false}else {newManagedObject.setValue(name, forKey: "name")newManagedObject.setValue(myDate, forKey: "datum")}// Save the context.var error: NSError? = nilif !context.save(&error) {abort()}}It occurs a crash in the function cellForRowAtIndexPath if markCell == true. If markCell == false (step into else) all works perfect.If I run this function:func saveName(name: String) {let myDate:NSDate = NSDate()let context = self.fetchedResultsController.managedObjectContextlet entity = self.fetchedResultsController.fetchRequest.entity!let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName(entity.name!, inManagedObjectContext: context) as NSManagedObjectnewManagedObject.setValue(name, forKey: "markedCell")markCell = falsenewManagedObject.setValue(name, forKey: "name")newManagedObject.setValue(myDate, forKey: "datum")// Save the context.var error: NSError? = nilif !context.save(&error) {abort()}}no crash occurs but than I also added a value to markedCell. I only want to add a value into markedCell if the bool is set to true (the user pressed a button -> bool will be set to true and func saveNamewill be called).Load data from core data (create UITableViewCell)://Get tasklet context = self.fetchedResultsController.managedObjectContextlet object = self.fetchedResultsController.objectAtIndexPath(indexPath) asNSManagedObjectvar taskString:NSStringtaskString = object.valueForKey("name") as Stringcell.textLabel!.text = object.valueForKey("name") as? String//Set accessory typevar request:NSFetchRequest = NSFetchRequest(entityName: "Person")request.predicate = NSPredicate(format:"markedCell = %@", taskString)var results : [NSManagedObject] = context.executeFetchRequest(request, error: nil) as [NSManagedObject]if (results.count > 0) {//Element existscell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicatorprintln("Cell is marked")}else {//Doesn't existcell.accessoryType = UITableViewCellAccessoryType.Noneprintln("Cell isn't marked")}


Please provide the exact message you get upon the crash

2019年04月20日04分27秒

name is nil. But is the saving function correct?

2019年04月19日04分27秒

I will check it later at home. Are you sure the passed name variable has a value when the function gets called?

2019年04月19日04分27秒

Can you provide the whole (or all relevant parts) of the project?

2019年04月19日04分27秒

Could it be that you are not setting 'Datum' when your if statement is true and you are required to set it?Also I think it's good practise to subclass your NSManagedObject, check this link: code.tutsplus.com/tutorials/… (this is in obj-C) or andrewcbancroft.com/2014/07/17/…

2019年04月19日04分27秒