Re: Newbie question regarding Learning Cocoa
Re: Newbie question regarding Learning Cocoa
- Subject: Re: Newbie question regarding Learning Cocoa
- From: Ali Ozer <email@hidden>
- Date: Mon, 4 Jun 2001 08:51:23 -0700
I think most of these will be answred as you make your way through the
book, but some quick answers:
>
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?
Internally the process of doing NSLog() might cause autoreleasing of
objects, which would be "leaked" if you did not have a pool. (A small
leak by itself isn't so bad in an app which quits immediately, but
you'll also get a log indicating there is a leak...)
>
color = [[ NSColor redColor] retain]; why do you retain the
>
new NSColor object?
[NSColor redColor] is an autoreleased returned (or, to put it more
accurately, it's a value would ownership is not being transferred to
you, and the value might go away at some point later). All objects are
returned this way, except for return values from alloc, copy, and new
methods. So, you retain or copy such return values to hang on to them.
>
return self; why do you retain 'self'? Why do you
>
return it in an 'id'?
init methods return self; sometimes they change self by reallocating it
to be another object, and this returning of self allows this to happen.
Clients need to of course write code like:
obj = [[MyClass alloc] init];
>
[super dealloc]; what is DotView's parent, which you must
>
dealloc? Does deallocing simply mean calling DotView's parent's
>
dealloc method?
DotView's superclass is NSView. but regardless, all dealloc methods
should always call [super dealloc] at the end to get their superclass to
cleanup.
>
[colorWell setColor: color]; how can you be sure here that
>
'color' points to an existing object? Did you retain it somewhere else?
It was created in the init method above, and even retained, so it's
valid.
>
[[NSColor whiteColor] set] why don't you retain the new
>
whiteColor object here, as you did above?
Here we are just using it momentarily --- and autoreleased returns are
good in the calling context without having to retain or copy.
>
[[NSColor whiteColor] set] what is 'set'? Was it predefined
>
somewhere?
It's an NSColor method to set the color.
>
return [NSCalendarDate date] what is 'date'? Is it a function?
It's a class (factory) method on NSCalendarDate.
Ali