Re: Newbie question regarding Learning Cocoa
Re: Newbie question regarding Learning Cocoa
- Subject: Re: Newbie question regarding Learning Cocoa
- From: "Mike Harrison" <email@hidden>
- Date: Tue, 5 Jun 2001 10:58:58 -0400
So is this book written so poorly that it doesn't even fully explain the
code in the examples?
Not what I'd expect from an O'Reilly book.
----- Original Message -----
>
i too am relatively new to cocoa, but i'll try and help you :)
>
>
On Monday, June 4, 2001, at 03:17 pm, 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?
>
>
>
@"Hello world" is actually an NSString object, which I think might be
>
autoreleased, I'm not sure..
>
>
> 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?
>
> return self; why do you retain 'self'? Why do you
>
> return
>
> it in an 'id'?
>
> }
>
>
>
(id) is the general type for any object. You have to return an object.
>
Typically, all init functions return self, otherwise the following code
>
wouldn't work:
>
>
myDotView = [[DotView alloc] initWithFrame:aFrame]; (assuming aFrame is
>
defined).
>
>
>
> - (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?
>
> }
>
>
deallocating means you should release any class variables that you have,
>
otherwise they'll still be floating around in memory, but with nothing
>
pointing to them - this is a memory leak.
>
>
> - (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];
>
> }
>
>
I don't know, I'd have to look in more detail at the project.
>
>
> -(void)awakeFromNib {
>
> ...
>
> [[NSColor whiteColor] set] why don't you retain the new
>
> whiteColor object here, as you did above?
>
> ...
>
>
why would you need to retain it when you've just created it?
>
>
> -(void)drawRect: (NSRect)rect {
>
> ...
>
> [[NSColor whiteColor] set] what is 'set'? Was it predefined
>
> somewhere?
>
>
it'll be a message/function, yeah.
>
>
> p. 158:
>
>
>
> return [NSCalendarDate date] what is 'date'? Is it a function?
>
>
it's a convenience constructor. it allocates some memory, initialises an
>
NSCalendarDate, autoreleases it, and returns it.
>
>
i'd like to point you in the direction of some excellent articles:
>
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
>
http://www.cocoadev.com/index.pl?MemoryManagement
>
>
-- Finlay
>
_______________________________________________
>
cocoa-dev mailing list
>
email@hidden
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev