I’m a bit confused on how to correctly make a copy of a Core Data object (just attributes, not relationships). Say that I have my object A, with an NSNumber x, and a NSString s. Is this correct way to copy this:
MyObject* B = (MyObject*)[NSEntityDescription insertNewObjectForEntityForName:@"MyObject" inManagedObjectContext:moc];
B.x = A.x;
B.s = A.s;
Or this:
MyObject* B = (MyObject*)[NSEntityDescription insertNewObjectForEntityForName:@"MyObject" inManagedObjectContext:moc];
B.x = [A.x copy];
B.s = [A.s copy];
If I update the attributes of A in the future, I don’t want the attributes of B to change.
Both
NSNumberandNSStringare immutable; if A’s attributes changed in the future, it wouldn’t change the existingNSNumberandNSStringobjects, it would replace them. From that point of view, your first example is perfectly sufficient.In fact, your second example is likely to leak memory if you aren’t running under garbage collection, since you’re never releasing your copied versions of A’s attributes.