0

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.

zztop
  • 701
  • 1
  • 7
  • 20

2 Answers2

1

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.

Thien Chu
  • 90
  • 7
  • Turns out I had an NSStringnamed list as well as an NSManagedObject named list. Sometimes the compiler was able to figure out which was which but in this case it did not. – zztop Apr 29 '17 at 00:29
0

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*)
nayem
  • 7,285
  • 1
  • 33
  • 51