Re: Newbie question regarding Learning Cocoa
Re: Newbie question regarding Learning Cocoa
- Subject: Re: Newbie question regarding Learning Cocoa
- From: Eric Peyton <email@hidden>
- Date: Mon, 4 Jun 2001 10:47:38 -0500
On Monday, June 4, 2001, at 09:17 AM, Philippe de Rochambeau wrote:
Hello,
I am currently working through O'Reilly's Learning Cocoa and have
problems understanding the following Obj-C code:
On p. 55 :
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello world");
[pool release];
What is the purpose of creating an autorelease pool in the code
above if
you are simply printing 'Hello World' onscreen?
Since you don't knwo the internal implementation of NSLog, it is
always safe to wrap an "unknown" function in an autorelease pool,
such as this. Internally, NSLog might be creating thousands of
objects and setting them to autorelease. if this code was
print("Hello world\n");
you would not need the autorelease pool.
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?
Because you want to use it later ... see your later question on the
ivar color ...
return self; why do you retain 'self'? Why do you
return
it in an 'id'?
You return a completed instance (id) of yourself from your
initialization. You do not "retain" self, you return it.
}
- (void)dealloc
{
[color release];
[super dealloc]; what is DotView's parent, which you must
dealloc?
DotView's parent is some descendent of NSObject and dealloc'ing
your parent will eventually drop the retain count as as the retain
count drops to zero, you can reclaim the memory.
Does deallocing simply mean calling DotView's parent's
dealloc method?
Yes.
}
- (void)awakeFromNib
{
[colorWell setColor: color]; how can you be sure here that
'color' points to an existing object? Did you retain it somewhere else?
You did - in the initWithFrame: method above.
[sizeSlider setFloatValue:radius];
}
@end
-(void)awakeFromNib {
...
[[NSColor whiteColor] set] why don't you retain the new
whiteColor object here, as you did above?
No need to retain it, since you are immediately using it.
...
-(void)drawRect: (NSRect)rect {
...
[[NSColor whiteColor] set] what is 'set'? Was it
predefined somewhere?
- set is a method on NSColor. Read the docs to see what it does.
p. 158:
return [NSCalendarDate date] what is 'date'? Is it a function?
A class method on NSCalendarDate. Read the docs to see what it does.
Eric
Any help with the above would be much appreciated.
Philippe de Rochambeau
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev