6
votes

By default when creating NSManagedObject subclass file from data model, Core Data creates properties of type NSString (nonatomic, retain) for string type columns. I might be done due to performance consideration, however, I'm implementing Data Mapper pattern where core data NSManagedObject subclasses are separated from model classes. So when converting model classes to Core data NSManagedObject subclass, retain just assigns a pointer and not a copy. From the architecture point of view, it brakes logic separation between core data access and model classes, so I need to use copy instead of retain.

Is it the only way to do is manually to change properties in NSManagedObject subclasses?

1
Just in case you aren't aware of this - the copy method of a normal (non-mutable) NSString returns the same instance, so don't freak out your copying property doesn't actually copy anything when you test it.Monolo
Yes, I'm aware of that, already tested. "Copy" copies mutable type objects only. Also, interesting to note, "copy" property also sends copy messages to NSArray elements, so mutable strings will be copied if exist inside array.Centurion
Please, take a look at my comment below the answer. Today, tried to figure out the practical usage "copy" properties. I understand how they work, just never had a need to use NSMutableString, so copy in my experience is redundant and just for future protection.Centurion

1 Answers

4
votes

The only way to do this is to change the declaration of the property in an NSManagedObject subclass. Thankfully, Core Data then handles the rest automatically.