0
votes

When I create a NSManagedObject Subclass Employee,it has a property nameaccording the EntityDescription in xcdatamodelfile. And in the .m file, the code modify it using @dynamic like this:

@interface Employee (CoreDataProperties)

@property (nullable, nonatomic, retain) NSString *name;

@end   


@implementation Employee (CoreDataProperties)

@dynamic name;

@end

According to the Apple's Document:

Core Data dynamically generates efficient public and primitive get and set attribute accessor methods and relationship accessor methods for properties that are defined in the entity of a managed object’s corresponding managed object model. Therefore, you typically don’t need to write custom accessor methods for modeled properties.

According this, I think the CoreData Framework will create two method named name and setName:in the runtime. So I use such code to verify my thinking.

Employee *object = [NSEntityDescription   insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
object.name = @"1";
[[self class] showInstanceMethod:[Employee class]];

+ (void)showInstanceMethod:(Class)class {
unsigned int outCount;
//..show InstanceMethodList
Method *methodsList = class_copyMethodList(class, &outCount);
for (int i = 0; i < outCount; i ++) {
    SEL sel = method_getName(*methodsList);
    NSString *methodName = NSStringFromSelector(sel);
    NSLog(@"\nmethodName:%@\n", methodName);
    methodsList++;
}
}  

I'm sad it didn't log any method name like name or setName:.But I use this code object.name = @"1"; and didn't have any problem.

1

1 Answers

1
votes

When they say "dynamically" they really do mean it - the dynamic implementation seems to be provided only as and when the selector is called (directly or via valueForKey:). You can see this happening if you override resolveInstanceMethod: in your Employee class. Call the super implementation and log the selector name and return value. Presumably the method will be listed by class_copyMethodList at this point, though I've never checked.