Reputation: 537
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)")
}
}
Upvotes: 1
Views: 1739
Reputation: 2307
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.
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 {
}
}
Upvotes: 5