• 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
Re: A Data Object in Cocoa
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A Data Object in Cocoa


  • Subject: Re: A Data Object in Cocoa
  • From: Jens Bauer <email@hidden>
  • Date: Fri, 9 Jan 2009 15:53:29 +0100

Hi Nik,

self is a pointer, so you might want to change "self." into "self->"

I usually prefix arguments with an 'a' (a for Argument):

- (id)initWithLabel:(NSString *)aLabel pin:(NSString *)aPin seed: (NSString *)aSeed
{
self = [super init];
if(self)
{
label = aLabel;
pin = aPin;
seed = aSeed;
}
return(self);
}


However, if you do the above, you'll most likely run into problems regarding autoreleased objects.

Instead, you should...

	label = [aLabel retain];
	pin = [aPin retain];
	seed = [aSeed retain];

And your -dealloc method:

- (void)dealloc
{
	[seed release];
	[pin release];
	[label release];
	[super dealloc];
}

You may prefer having setters and getters:

- (void)setLabel:(NSString *)aLabel
{
	[aLabel retain];
	[label release];
	label = aLabel;
}

- (NSString *)label
{
	return(label);
}

Then you could simply use the much more 'in order':

- (id)initWithLabel:(NSString *)aLabel pin:(NSString *)aPin seed: (NSString *)aSeed
{
self = [super init];
if(self)
{
[self setLabel:aLabel];
[self setPin:aPin];
[self setSeed:aSeed];
}
return(self);
}


- (void)dealloc
{
	[self setSeed:NULL];
	[self setPin:NULL];
	[self setLabel:NULL];
	[super dealloc];
}


Love, Jens

On Jan 9, 2009, at 08:49, nik heger wrote:

I am trying to define a pure data object in Cocoa. This object doesn't do anything, it should just act as a container for three strings. This is generally a good design pattern, at least in Java.

In Cocoa, I am running into a lot of resistance. The constructor is rather complicated.

And the compiler complains about this:

- (id)initWithLabel:(NSString *)label pin:(NSString *)pin seed: (NSString *)seed {
if (self = [super init]) {
self.label = label; ///<----------- compiler complains
self.pin = pin;
self.seed = seed;
}
return self;
}


I get three warnings that say "local declaration of x overrides instance variables. I thought I could differentiate between the parameters and the instance variables using self.variable vs just variable.

What's the Cocoa way of doing this? Do I really have to name the parameters pLabel, pSeed, and pPin? Or would it be better to just keep that stuff in a Dictionary object?

Thanks for any help,

	Nik
_______________________________________________

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


_______________________________________________

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: A Data Object in Cocoa
      • From: Thomas Davie <email@hidden>
References: 
 >A Data Object in Cocoa (From: nik heger <email@hidden>)

  • Prev by Date: QCView to fullscreen
  • Next by Date: Re: A Data Object in Cocoa
  • Previous by thread: A Data Object in Cocoa
  • Next by thread: Re: A Data Object in Cocoa
  • Index(es):
    • Date
    • Thread