0

The code is easy to understand, but output is not.

Screen of xcdatamodel editor:

cl.ly/image/3U0P1W301K06

The type of deleted is String.

@interface Row : NSManagedObject
    (...)
    @property (nonatomic, retain) NSString * deleted;
@end

@implementation Row
    (...)
    @dynamic deleted;
@end

Somewhere else in code:

Row *row = [self.fetchedResultsController objectAtIndexPath:indexPath];

NSString* fixedTypeDeletedClassString = NSStringFromClass([row.deleted class]);
NSString* KVCDeletedClassString = NSStringFromClass([[row valueForKeyPath:@"deleted"] class]);

NSLog(@"%@ is %@", row.deleted, fixedTypeDeletedClassString);
NSLog(@"%@ is %@", [row valueForKeyPath:@"deleted"], KVCDeletedClassString);

The output is:

2014-05-07 17:29:26.170 iOSplayground[20814:90b] false is __NSCFString
2014-05-07 17:29:26.171 iOSplayground[20814:90b] 0 is __NSCFNumber

Why the class of the property is different?

Class is different, but at least looks like properly casted, but there is a worse case:

2014-05-07 17:29:26.172 iOSplayground[20814:90b] true is __NSCFString
2014-05-07 17:29:26.172 iOSplayground[20814:90b] 0 is __NSCFNumber

I've been fast prototyping without Row class at first, and used NSManagedObject with KVC, and I've lost a lot of time on debuging why there is a property of class that I didn't use.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
karolszafranski
  • 702
  • 8
  • 17

1 Answers1

1

The question is different, but the underlying problem is the same as in

Core Data NSPredicate "deleted == NO" does not work as expected

Calling a Core Data property "deleted" conflicts with the isDeleted method of NSManagedObject.

In your case, row.deleted seems to fetch the string property, whereas [row valueForKeyPath:@"deleted"] returns YES or NO, depending on whether the object has been deleted from the managed object context.

Unfortunately, the documentation is pretty vague on what Core Data property names are allowed, and the Model Inspector does not warn in this case. Another example is "updated", which conflicts with the isUpdated method.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382