0

Started working with booleans trying to save them within core data and figured I could do a simple true/false save object but after reading I am unsure of whether or not they added the ability to do this in swift or not. I read another post Swift + CoreData: Can not set a Bool on NSManagedObject subclass - Bug?. Reading through it led me to believe that they changed this so that it should be able to save simple booleans based on the edit from the first answer.

So I created the class with Bool instead of NSNumber, and tried to set the object true and save the object. It doesn't seem to work. So my question is do you have to set the object with NSNumber still?

The issue I am facing is that when I try to recall the data using a fetch request after saving the Bool as true, with the default value set to No which should = false if I understand correctly, I should be able to recall which attribute is true through the getter that I created as a var that loops to check what the object is and returns it as a string. This however isn't working.

Community
  • 1
  • 1
Matthew
  • 73
  • 2
  • 13
  • 1
    `Bool` can be automatically bridged to `NSNumber` back and forth, so it should work. "It doesn't seem to work" - what problem are you experiencing? – Kirsteins Jul 28 '15 at 10:16
  • Ok maybe the issue is with how I am trying to record the object? `let entity = NSEntityDescription.entityForName("Class", inManagedObjectContext: classMOC!) let newObject = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:classMOC!) newObject.setValue(true, forKey: "isHunter")` – Matthew Jul 28 '15 at 10:24
  • Make sure you are saving the context after changing the value. – Kirsteins Jul 28 '15 at 10:26
  • After using the method I presented in the above comment, when I try to recall that through the loop check that I use to read out a string for my tableview based on the true/false check, I get a error stating that optional returned nil – Matthew Jul 28 '15 at 10:26
  • sorry i have a save in my method i am trying to test with – Matthew Jul 28 '15 at 10:27
  • I linked the uncompleted second button – Matthew Jul 28 '15 at 10:27
  • `let entity = NSEntityDescription.entityForName("Class", inManagedObjectContext: classMOC!) let newObject = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:classMOC!) newObject.setValue(true, forKey: "isWarrior") classMOC?.save(nil)` – Matthew Jul 28 '15 at 10:28
  • I do have the save method sorry for linking the wrong one...I thought this should save the true object...but it only returns optional as nil error – Matthew Jul 28 '15 at 10:28
  • With the save method in to save the context it doesn't work and I am unsure as to why. All I have been reading suggests that the methods implemented should work – Matthew Jul 28 '15 at 12:21
  • Any other suggestions on direction I should try would be greatly appreciated! – Matthew Jul 28 '15 at 14:01

0 Answers0