Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

@property question



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

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.