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.
The code I am using within my button is posted below. The issue I am facing however is that when I try to retrieve this stored data to use it returns
let entity = NSEntityDescription.entityForName("Class", inManagedObjectContext: classMOC!)
let newObject = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:classMOC!)
newObject.setValue(true, forKey: "isHunter")
I am trying to use a convenience method that allows me to return strings for the true/false check in my tableview. That code is as follows:
import Foundation
import CoreData
class Class: NSManagedObject {
@NSManaged var isDruid: Bool
@NSManaged var isHunter: Bool
@NSManaged var isMage: Bool
@NSManaged var isPaladin: Bool
@NSManaged var isPriest: Bool
@NSManaged var isRogue: Bool
@NSManaged var isShaman: Bool
@NSManaged var isWarlock: Bool
@NSManaged var isWarrior: Bool
@NSManaged var classfaced: NSSet
@NSManaged var classSelection: NSSet
@NSManaged var playersclass: NSSet
@NSManaged var totalclassfaced: NSSet
var className: String {
get {
if isDruid{
return "Druid"
} else {
if isHunter {
return "Hunter"
} else {
if isMage {
return "Mage"
} else {
if isPaladin {
return "Paladin"
} else {
if isPriest {
return "Priest"
} else {
if isRogue {
return "Rogue"
} else {
if isShaman {
return "Shaman"
} else {
if isWarlock {
"Warlock"
} else {
if isWarrior {
return "Warrior"
}
}
}
}
}
}
}
}
}
return ""
}
}
}
Finally my method I am using to recall this in the tableview is:
let aDeck = savedDecksClass[indexPath.row]
cell.detailTextLabel?.text = aDeck.className
aDeck is an array that is supposed to store the Boolean objects for recall. I get a fatal error : array index out of range.