1

I'm using core data and when I try to read my properties, they are read as numbers such as 0 and 1. Here is my Core Data set up. My end goal is to save a boolean to true or false and then read read it as such. Am I doing something wrong in my setup, or missing something? I've looked that the post iPhone: Save boolean into Core Data and besides being in OBJ C, I believe booleans are available now in the Datamodel options.

 func saveFilters(bathAny: Bool, bath1: Bool, bath2: Bool, bath3: Bool, bath4: Bool){
    let context = getContext()

    let entity = NSEntityDescription.entity(forEntityName: "FilterCoreDataObj", in: context)
    let transc = NSManagedObject(entity: entity!, insertInto: context)

    //SET ENTITY VALUES 

    transc.setValue(bathAny, forKey: "bathAny")
     transc.setValue(bath1, forKey: "bath1")
     transc.setValue(bath2, forKey: "bath2")
     transc.setValue(bath3, forKey: "bath3")
     transc.setValue(bath4, forKey: "bath4")


    do {
        try context.save()
        print("saved")
    } catch let error as NSError {
        print("could not save \(error), \(error.userInfo)")
    } catch {

    }
}

Read Func

  func getTranscriptions(bathAny: Bool, bath1: Bool, bath2: Bool, bath3: Bool, bath4: Bool) {
    let fetchRequest: NSFetchRequest<FilterCoreDataObj> = FilterCoreDataObj.fetchRequest()

    do {
        let searchResults = try getContext().fetch(fetchRequest)
        print("num of results = \(searchResults.count)")
        for trans in searchResults  {
            print("bath 1 \(trans.value(forKey: "bath1"))")


        }


    } catch {
        print("Error with request: \(error)")
    }

}
Community
  • 1
  • 1
bradford gray
  • 537
  • 2
  • 5
  • 18
  • 2
    Possible duplicate of [iPhone: Save boolean into Core Data](http://stackoverflow.com/questions/2937945/iphone-save-boolean-into-core-data) – keithbhunter Mar 08 '17 at 16:39
  • Well one this is swift, and core data has changed a lot in Swift 3. It also seems that in the datModel, it lets you set a property up as a boolean. – bradford gray Mar 08 '17 at 16:46

1 Answers1

3

If you select an attribute, then open the 'Data Model inspector' tab there is a 'use scalar type' option that will allow you to work directly with a Bool type instead of the underlying NSNumber value.

enter image description here

I can confirm that in Swift 3 (with the auto generated Managed Object subclass) this works out of the box.

If you create your own NSManagedObject subclass (or extension) then you'll need to expose the property.

p.s. As a side note, I'm guessing that you're project doesn't currently use any subclasses or extensions to your NSManagedObjects. If you're new to using CoreData this can be a confusing topic because CoreData tools have changed dramatically with Xcode 8.

As a short intro, you can either manually create a subclass (or extension) for your entities (or my preference - have Xcode automatically generate them for you).

In your case an extension would look like this:

extension FilterCoreDataObj {
    @NSManaged public var bathAny: Bool
    @NSManaged public var bath1: Bool
    @NSManaged public var bath2: Bool
    @NSManaged public var bath3: Bool
    @NSManaged public var bath4: Bool
} 

Then using the entity becomes a lot easier because you can access the properties directly rather than using strings for key paths:

func saveFilters(bathAny: Bool, bath1: Bool, bath2: Bool, bath3: Bool, bath4: Bool){

    let context = getContext()

    let transc = FilterCoreDataObj(context: context)
    //SET ENTITY VALUES

    transc.bathAny = bathAny
    transc.bath1 = bath1
    transc.bath2 = bath2
    transc.bath3 = bath3
    transc.bath4 = bath4

    do {
        try context.save()
        print("saved")
    } catch let error as NSError {
        print("could not save \(error), \(error.userInfo)")
    } catch {

    }
}
MathewS
  • 2,267
  • 2
  • 20
  • 31
  • Thank you for the life saver answer!! – bradford gray Mar 08 '17 at 17:41
  • I was wondering if you could answer one more question about core data. So I'm trying to save an array of custom objects with core data. The custom objects already exist, but I'm not sure how to go about writing and saving them. Can I just save the custom object? And if so, would it be an attribute,? And how would I changed the type to that? Or Would I make a class of that object, and set up every attribute to match what exists in the object? Thank you for your help. I'll open this in another question if you want so you can answer there. – bradford gray Mar 10 '17 at 15:47
  • If you don't want to model the custom object as it's own CoreData entity, you can save the custom object using a `transformable` attribute. The short answer here is to have your custom object implement `NSCoding` protocol so that your core data store can translate your object into a binary blob to store. I tried finding a relevant question to link, but surprisingly hasn't been asked (for Swift) yet. – MathewS Mar 10 '17 at 15:57
  • I'll post it in a question I guess and link it after I'm done – bradford gray Mar 10 '17 at 16:07
  • I actually decided to model the objects and I ran into an issue. Here is the question. http://stackoverflow.com/questions/42773539/why-i-can-only-fetch-one-object-from-an-array-core-data-swift-3. Btw, your help has been extremely helpful. If you ever need any code review or anything, let me know. – bradford gray Mar 13 '17 at 21:17