Skip navigation.
 
ml@property question
FROM : Craig Hopson
DATE : Mon May 12 23:30:16 2008

Hi,

I'm looking for a little understanding...  well, someone to help me 
understand an issue I'm having with properties.

I have a class something like...
@interface Foo : NSObject
{
   NSObject*    field1;
   NSObject*    field2;    
   NSObject*    field3;
   NSObject*     field4;
}

@property( nonatomic, retain ) NSObject* field1;
@property( nonatomic, retain ) NSObject* field2;
@property( nonatomic, retain ) NSObject* field3;
@property( nonatomic, retain ) NSObject* field4;

// other methods...

@end

@implementation Foo

@synthesize field1;
@synthesize field2;
@synthesize field3;
@synthesize field4;

// other methods...
@end

In another class I have a mutable array and a method to add things to 
it....
@interface Bar : NSObject
{    
   NSMutableArray* fieldArray;
}

@property( retain ) NSMutableArray* fieldArray;

- (void)addFoo:( Bar* )inFoo;
- (Foo*)fooAtIndex:( NSUInteger )index;


@implementation Bar
@synthesize fieldArray;

// in the -init method storage is set up for fieldArray

// return a Foo from the array
- (Foo*)fooAtIndex:( NSUInteger )index
{
   return [ fieldArray objectAtIndex:index ];
}

If I add foos to the array like this...
- (void)addFoo:( Bar* )inFoo
{
   [ fieldArray addObject:inFoo ];
}
... everything is fine.

If, however, I implement addFoo like this...
- (void)addFoo:( Bar* )inFoo
{
   [ self.fieldArray addObject:inFoo ];
}
... the contents of foo are invalid when I return one using fooAtIndex:

So, the question is, what is the difference between using self. 
notation and not?  I have declared the properties to retain the 
objects.  The compiler is happy, I just die at run time when I attempt 
to access the contents of a Foo.  It looks like the contents of Foo 
are not retained in the second case.  I've concluded that it's a bad 
thing to use the self. notation in general usage, but it is not at all 
clear to me why this is true and I cannot find any warning against 
this in the Obj C docs on Apple's web site.

Thanks for any insight.
-Craig

Related mailsAuthorDate
ml@property question Craig Hopson May 12, 23:30
mlRe: @property question Shawn Erickson May 12, 23:42
mlRe: @property question mmalc crawford May 12, 23:52
mlRe: @property question Craig Hopson May 13, 00:19
mlRe: @property question Phoenix Draco May 13, 00:42
mlRe: @property question Clark Cox May 13, 00:51
mlRe: @property question Quincey Morris May 13, 01:20
mlRe: @property question Craig Hopson May 13, 18:49
mlRe: @property question Roland King May 14, 02:41
mlRe: @property question Kyle Sluder May 14, 04:41
mlRe: @property question Chris Hanson May 14, 05:11