user1066524
user1066524

Reputation: 373

Problems with Boolean type attribute in Core Data with predicates in Xcode

Okay, so I already tried the examples on apple's SDK developer pages and it didn't work. I tried the examples from previous questions on stackoverflow like :

NSEntityDescription  entitydesc = [NSEntityDescription entityForName:@"Model" inManagedObjectContext:context];
NSFetchRequest request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];

//tried this style of predicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"accessible == %@", [NSNumber numberWithBool:YES]];

//and this one

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"accessible == YES"];
[request setPredicate: predicate];

NSError *error;
NSArray *matchingData = [context executeFetchRequest: request error: &error];

both of which didn't work. In debugger the matchingData shows 0 objects when it should have many.

All my other fetchrequests have worked perfectly fine. This is the only one giving me problems.

The attribute is listed as type Boolean in the .xcdatamodel

The attribute is listed as this under the entity's header file:

@property (nonatomic, retain) NSNumber * accessible;

The data was entered into the core data database as follows:

NSNumber *accessibleFieldValue  = [NSNumber numberWithBool:YES];
[newModel setValue: accessibleFieldValue forKey:@"accessible"];

I've checked and there are no nil values entered in the sqlite database.

What should I do?

Upvotes: 0

Views: 1773

Answers (3)

Yedy
Yedy

Reputation: 2126

It works fine with the new literals:

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"done = %@",@NO];

Upvotes: 1

user1066524
user1066524

Reputation: 373

I ended up going to the database in sqlite database browser and changing the type for the field value to Numeric. For some reason core data entered the data as an incompatible data type.....don't really know why, but I have a feeling this might be an Xcode bug. As I said, it works now after I changed the field type. I don't think I will be using boolean values in core data any time soon.....at least not until I understand why this was a problem.

Upvotes: 0

Kjuly
Kjuly

Reputation: 35141

NSNumber *accessibleFieldValue  = [NSNumber numberWithBool:YES];
[newModel setValue:accessibleValue forKey:@"accessible"];

accessibleFieldValue or accessibleValue? If you're sure this snippet of code is right, that's where you did wrong.

And b.t.w, you can set value simply like:

newModel.accessible = [NSNumber numberWithBool:YES];

Upvotes: 0

Related Questions