Q: NSManagedObject key exists in the entity description but when trying to access it the app crashes with "this class is not key value coding-compliant" I have a NSManangedObject that displays all the correct attributes in the entity description. But whenever I try to access one of the attributes it crashes saying it is not key-value coding compliant.
I have triple checked my datamodel and the entity class and everything looks correct.
I'm not sure if it makes a difference but I am accessing the object from a notification. Here is an example:
NSDictionary *dict = notification.userInfo;
NSSet *set = [dict objectForKey:NSInsertedObjectsKey];

for (NSManagedObject* managedObject in set) {
    NSLog(@"%@", managedObject.entity.attributesByName);
    if([managedObject.entity.attributesByName valueForKey:@"defaultid"] != nil) {
        NSLog(@"defaultid: %@", [managedObject valueForKey:@"defaultid"]);
    }
}

Will output:
{
    defaultid = "(<NSAttributeDescription: 0x175c90>), name defaultid, isOptional 1, isTransient 0, entity MyEntity, renamingIdentifier defaultid, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 100 , attributeValueClassName NSNumber, defaultValue 0";
    title = "(<NSAttributeDescription: 0x175ce0>), name title, isOptional 1, isTransient 0, entity MyEntity, renamingIdentifier title, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)";
}

And then crash with the following:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<_NSObjectID_64_6 0x6119020> valueForUndefinedKey:]: this class is not key value coding-compliant for the key defaultid.'

Any insight would be greatly appreciated.
EDIT:
The notification is from iCloud which calls this method:
- (void)mergeChangesFrom_iCloud:(NSNotification *)notification {
    NSManagedObjectContext* moc = [self managedObjectContext];
    [moc performBlock:^{
        [self mergeiCloudChanges:notification forContext:moc];
    }];
}

The above code is in the mergeiCloudChanges method.
EDIT 2 (SOLVED):
So it turns out I made a stupid mistake. I thought [dict objectForKey:NSInsertedObjectsKey] returned a set of managedObjects but what it does is return a set a managedObjectIDs.
This works:
NSDictionary *dict = note.userInfo;
NSSet *set = [dict objectForKey:NSInsertedObjectsKey];
for (NSManagedObjectID* managedObjectID in set) {
    NSManagedObject *newObject = [moc existingObjectWithID:managedObjectID error:nil];
}

Thanks to everyone for their time.

A: From what I can tell in the entity description, you have defaultid marked as optional. Does it exist for the entity you are accessing? If you turn off optional and give it a default value, what would happen?
