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.

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.

ciccioska
  • 1,291
  • 16
  • 22
Matthew
  • 73
  • 2
  • 13
  • 2
    It's not a good idea to name a class `Class`, try something like `CharacterClass`. And why don't you just use a `className: String` property on that class instead of having all those bools and that unsightly computed property? – Patrick Lynch Jul 29 '15 at 08:19
  • 1
    oiii! what an ugly embedded `if...else...` statement. :( and it looks buggy as well... – holex Jul 29 '15 at 09:13
  • @PatrickLynch I was trying to avoid using queries by having a simple loop to check whether the object was true/false. – Matthew Jul 29 '15 at 13:41
  • @holex I used an example that was given to me by another programmer who recommended I use this route. – Matthew Jul 29 '15 at 13:41

1 Answers1

0

Core Data when works with Bool convert it in NSNumber, this answer can help you:

Swift + CoreData: Can not set a Bool on NSManagedObject subclass - Bug?

Community
  • 1
  • 1
ciccioska
  • 1,291
  • 16
  • 22
  • See I was reading through that article myself and the second answer led me to believe that it was a bug at the time that core data couldn't save the true/false objects as they do have the option for scalers now. And that was back last year sometime if I remember correctly. I was hoping to avoid . syntax with nsnumber as like the poster in there said it shouldn't require you to use NSNumber at all. – Matthew Jul 29 '15 at 13:45