• 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: NSDate And Super Class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDate And Super Class


  • Subject: Re: NSDate And Super Class
  • From: Richard Good <email@hidden>
  • Date: Wed, 25 Feb 2009 09:03:27 -0800

Thanks for looking at the code. I tried to simplify things for the example so I removed as much as possible (maybe too much) that I thought was not directly relevant. However making all of your suggested changes and maybe an extra retain or two, still yields an "Out of scope" problem in the debugger. In fact at NO time during the initialization does the debugger show the super types NSDate's instance variable in scope.
-(DateTest*) init {
if (self=[super init]) {
aDate =[[NSDate date]retain];
return self;
}
return nil;
}
-(DatesubClass*) init {
if (self =[super init]) {

testStr= @"Test";
return self;
}
return nil;


}
@end

DatesubClass* theDate = [[[DatesubClass alloc]init]retain];


DatesubClass* datesubClass = [[DatesubClass alloc]init]; would allocate the On Feb 24, 2009, at 7:01 PM, Andy Lee wrote:

On Feb 24, 2009, at 8:25 PM, Richard Good wrote:
-(DateTest*)	init	{
	[super init];
	aDate =[NSDate date];
	return self;
}

Wait a minute. I just figured out you're saying the *debugger* says datesubClass.aDate is out of scope, not the compiler as I thought. The reason is that you're not retaining aDate in the init method. It gets deallocated prematurely and the pointer is invalid, hence "out of scope". Try


	aDate = [[NSDate date] retain];

If it's unclear why you need to do this, you need to study memory management -- it's a prominent chapter in the docs, you should be able to find it, plus it's linked to many, many times in the list archives.

There are other coding issues: init methods should return id, and they should check the result of [super init]. There's a chapter on "writing initializers" or some such -- look for that too.

--Andy


_______________________________________________

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: NSDate And Super Class
      • From: Andy Lee <email@hidden>
References: 
 >NSDate And Super Class (From: Richard Good <email@hidden>)
 >Re: NSDate And Super Class (From: Andy Lee <email@hidden>)

  • Prev by Date: Re: Subclassing with more restrictive initializers
  • Next by Date: Re: Subclassing with more restrictive initializers
  • Previous by thread: Re: NSDate And Super Class
  • Next by thread: Re: NSDate And Super Class
  • Index(es):
    • Date
    • Thread