Does a setter of a CoreData-attribute retains? or copies the value or what does the setter? I want to know if I have to (auto-)release the value that I am putting into the setter.
I've done some quick tests on that. Assuming the following model:
+----------+
| Class A |
+----------+
| v :int32 |
+----------+
The generated ManagedObject then looks like this:
//A.h
@interface A : NSManagedObject
{
}
@property (nonatomic, retain) NSNumber * v;
//...
//A.m
@implementation A
@dynamic v;
...
Ok, the property is marked with retain, so if I set a NSNumber to v the NSNumber should be retained. but it is not (I think). I did the following:
A *a = ... ;
NSNumber *retainCheck = [[NSNumber alloc] initWithInt:23];
NSLog(@"retainCheck1: %i",[retainCheck retainCount]);
a.v = retainCheck;
NSLog(@"retainCheck2: %i",[retainCheck retainCount]);
NSLog(@"retainCheck3: %i",[a.v retainCount]);
NSLog(@"pointer1: %#x",retainCheck);
NSLog(@"pointer2: %#x",a.v);
this produces the following output:
retainCheck1: 1
retainCheck2: 1
retainCheck3: 1
pointer1: 0x62f068
pointer2: 0x62f068
Both NSNumbers are the same NSNumber-instance (same pointer-value -> so it does no copy or sth like that) and the NSNumber doesn't seem to be retained by the setter.
Am I doing sth. wrong in my test? or is the behaviour of NSManagedObject-setters not the same as described in the header?
edit: I am calling retainCount because I wanted to observer a retain not release or autorelease. The observed behaviour can be explained by the flyweight-pattern linked by Abizern