Re: Newbie question regarding Learning Cocoa
Re: Newbie question regarding Learning Cocoa
- Subject: Re: Newbie question regarding Learning Cocoa
- From: Scott Anguish <email@hidden>
- Date: Mon, 4 Jun 2001 11:46:50 -0400
On Monday, June 4, 2001, at 10:17 AM, Philippe de Rochambeau wrote:
What is the purpose of creating an autorelease pool in the code above if
you are simply printing 'Hello World' onscreen?
The autorelease pool is created so that you can use foundation and other
classes and take advantage of the memory management stuff. In this
particular case, there is no need for it, but it isn't hurting
anything. It's what the template generates.
I don't have a copy of the book, so I can't put some of these into
context... but
On page 136,
#import "DotView.h"
@implementation DotView
- (id)initWithFrame: (NSRect)frame
{
self = [super initWithFrame:frame];
center.x = 50.0;
center.y = 50.0;
radius = 10.0;
color = [[ NSColor redColor] retain]; why do you retain the
new
NSColor object?
the color is stored in the DotView instance so that you can redraw
from it.
I'm not sure what the memory management notes are in Learning
Cocoa, but here is a good article that explains it.
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
return self; why do you retain 'self'? Why do you
return
it in an 'id'?
You're not retaining self.
It is returned as an id because initWithFrame: returns a generic
object to it...
}
- (void)dealloc
{
[color release];
[super dealloc]; what is DotView's parent, which you must
dealloc? Does deallocing simply mean calling DotView's parent's
dealloc method?
you're not sending it to it's parent, so much as up the inheritance
chain. In this specific subclass of DotView we have added an instanced
variable (color) which we deallocate. We then pass the dealloc message
to the superclass of the DotView (NSView) which will then dealloc
anything that it created or saveds, and pass it up to NSResponder and so
on..
}
- (void)awakeFromNib
{
[colorWell setColor: color]; how can you be sure here that
'color' points to an existing object? Did you retain it somewhere else?
[sizeSlider setFloatValue:radius];
}
@end
-(void)awakeFromNib {
...
[[NSColor whiteColor] set] why don't you retain the new
whiteColor object here, as you did above?
...
-(void)drawRect: (NSRect)rect {
...
[[NSColor whiteColor] set] what is 'set'? Was it predefined
somewhere?
set is an instance method on NSColor.. so you create an instance of
NSColor with the "whiteColor" class method, and then you set the color.
It's a little hard to see exactly what you're setting it on.. but
because it's being done in the drawRect: method you can be sure that it
is setting it for the current view (in the graphics context actually)
p. 158:
return [NSCalendarDate date] what is 'date'? Is it a function?
Any help with the above would be much appreciated.
date, like whiteColor, redColor and other methods preceeded in the
docs with a + are class methods. They will usually return an instance
of the class set to some value that hopefully relates to what the method
you called (data == current date... etc..)