New Way to Override NSManagedObject Properties

Published by malhal on

There is a new, yet not very well-known way to override NSManagedObject properties without needing to manually call KVO methods willChangeValueForKey etc. This is achieved using dynamic accessors prefixed with managedObjectOriginal as follows:


@interface Department : NSManagedObject
@property (nullable, nonatomic, copy) NSString *name;


@interface Department (DynamicAccessors)
- (NSString *)managedObjectOriginal_name;
- (void)managedObjectOriginal_setName:(NSString *)newName;

@implementation Department

@dynamic name;

- (NSString *)name
    // invoke the dynamic implementation of name
    NSString *name = [self managedObjectOriginal_name];
    // your custom code
    return name;

- (void)setName:(NSString *)name
    // invoke the dynamic implementation of setName
    [self managedObjectOriginal_setName:(NSString *)name;
    // your custom code


As seen at bottom of What’s New in Core Data in macOS 10.12, iOS 10.0, tvOS 10.0, and watchOS 3.0.


Leave a Reply

Avatar placeholder