• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
@property question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

@property question


  • Subject: @property question
  • From: Craig Hopson <email@hidden>
  • Date: Mon, 12 May 2008 15:30:16 -0600

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: @property question
      • From: mmalc crawford <email@hidden>
    • Re: @property question
      • From: "Shawn Erickson" <email@hidden>
  • Prev by Date: Re: NSTask waitForDataInBackgroundAndNotify and waitUntilExit
  • Next by Date: What's with ICAImportImage?
  • Previous by thread: Re: NSTask waitForDataInBackgroundAndNotify and waitUntilExit
  • Next by thread: Re: @property question
  • Index(es):
    • Date
    • Thread