• 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
initializing ivars
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

initializing ivars


  • Subject: initializing ivars
  • From: Daniel Child <email@hidden>
  • Date: Sun, 02 Sep 2007 12:15:56 -0400

Hi Shawn,

Actually, sorry, that init was typed into mail. The real init does not have the extra semicolon. Here it is (and I still hope it is standard).

- (id) init {
	self = [super init];
	return self;
}

However, your messages got me thinking about something I found odd running through the debugger.

If my instance variables consist of something like NSStrings, and I use standard accessor methods (return the ivar for getter, release ivar, then set ivar to copy of parameter for setter), then everything works fine.

But if the ivar consists of an NSMutableArray (or presumably any other collection), then I have to somehow initialize the array or it will have 0x0 memory assigned to it and will not respond to attempts to add objects. I find that surprising. If I alloc memory for the class instance, why wouldn't memory be created for the instance variable in one case (arrays) and not the other (non-collection data types)?

I think the answer is that since to set the NSStrings you are initially releasing nil (also 0x0 memory status), then it's not a problem since it will be copying the address of the variable I send as a parameter in the setter. But to add to an array, it must first exist.

Which is why I was asking about a standard way to "reset" or in some cases initialize instance variables. Here was my reset method, which worked. But I don't see other people doing something similar....

- (void) reset
{
	[self setInput: [NSString string]];
	[self setSyllables: [NSMutableArray array]];
	[self setCandidate: [NSString string]];
	[self setRemainder: [NSString string]];
	[self setIsSyllable: NO];
	[self setWasSyllable: NO];
	[self initializeParser];
	// I SHOULDN'T HAVE TO REINITIALILZE THE PARSER,
	// BUT DURING RESET possibilities BECOMES INVALID FOR SOME REASON
	// NEED TO ADDRESS THIS ISSUE LATER
}

Curiously, a parser that gets initialized by placing a bunch of strings into an NSSet (one of the ivars), disappears unless I reinitailize it. I think it's because I use a convenience constructor ( possibilities = [NSSet setWithObjects:), so I guess it gets autoreleased after some unspecified amount of time. Probably better to go alloc, init, and then set to nil in the dealloc.

But as for the value initialization I was talking about (input, syllables etc.)....does the above approach look strange?

Daniel

On Sep 1, 2007, at 12:31 PM, Shawn Erickson wrote:

Ah sorry I thought your first sentence "I think the init method is pretty standard." was "I think the _dealloc_ method is pretty standard." when I first read it because I was focusing on the issue you had in dealloc.

Anyway your init method is in a way non-standard since it doesn't do anything beyond what you would inherit from your super class, so you don't need to define or declare it.

_______________________________________________

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: initializing ivars
      • From: "Shawn Erickson" <email@hidden>
    • Re: initializing ivars
      • From: "Shawn Erickson" <email@hidden>
References: 
 >Re: file scan results in EX_BAD_ACCESS (From: Daniel Child <email@hidden>)
 >Re: file scan results in EX_BAD_ACCESS (From: Shawn Erickson <email@hidden>)

  • Prev by Date: Re: my preferences sheet appears under XCode not under the app ...
  • Next by Date: Re: core data app questions - help!
  • Previous by thread: Re: file scan results in EX_BAD_ACCESS
  • Next by thread: Re: initializing ivars
  • Index(es):
    • Date
    • Thread