zztop
zztop

Reputation: 781

Objective-C: Set boolean value in core data

In my data model, in an entity, I have an attribute that is a boolean. In the xcmodeldatad file, it is listed as:

need sync Boolean. In the NSmanagedobject file it is as follows:

.h file: @property (nonatomic, strong) NSNumber *needsync;//bool
.m file @dynamic needsync;

When saving the record after an edit, I use the following code that I have used numerous times before:

[list setValue:@0 forKey:@"needsync"];

However, it is throwing an exception when it reaches this line of

NSUnknownKeyException', reason: '[<__NSCFString 0x17409c4d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key need sync.'

This is really baffling me and I wonder if anyone can spot error or has had this happen before.

Thank you.

Upvotes: 0

Views: 589

Answers (2)

nayem
nayem

Reputation: 7595

What's the type of your list dictionary? Is it NSMutableDictionary<NSString*, NSNumber*> ? If so then you might have mismatched type between your Entity Attribute and Property Declaration.

Or if your dictionary is of type NSMutableDictionary<NSNumber*, NSNumber*> then try this:

[list setObject:@0 forKey:needsync]; // don't make `needsync` a NSString* object, it's already an object type (NSNumber*)

Upvotes: 0

Thien Chu
Thien Chu

Reputation: 90

Check your code again.

The crash log shows that your object is a NSString, instead of YourList object, so, it doesn't has needsync property => crash.

Upvotes: 1

Related Questions