Re: Initializing an Array
Re: Initializing an Array
- Subject: Re: Initializing an Array
- From: jgo <email@hidden>
- Date: Tue, 29 Jan 2002 20:31:18 -0800
>
No, no, no! 8^0 This is not the way to do this.
>
You don't seem to understand what classes are.
The Italians have an expression having to do with the
need for certain persons to eat a lot of bread.
Yes I most certainly do understand what classes are, what
self is and what super is. super merely says, "Don't look
in the self instance for this method, but start looking in
the class from which self is derived." It's a simple thing
without mystery.
What I don't understand are the conventional incantations in Objective C.
>
You should be doing it like this:
>
// init
>
-(id)init {
>
// first assign the result of [super init] to self
>
if (self = [super init]) {
>
// only if self is not nil do we do our subclass' init
>
x = [[SomeClass alloc] init]; // for example
>
}
>
return self; // we return self which might be nil
>
}
Thank you. You & the docs have got some weird issues with writing
easily readable, balanced braces, debugger-examinable code & such,
but otherwise this is exactly the sort of example I would expect
to see in the Objective C book, but do not.
-(id)init
{
// first assign the result of [super init] to self
self = [super init];
if (self != nil)
{
// only if self is not nil do we do our subclass' init
x = [[SomeOtherClassWhichWasIntroducedForSomeUnknownReason
alloc] init]; // for example
// but wait, we are in the init for ThisClass, already
// so we need to init self's iVars...
// unless you meant "x" to be self's iVar?
}
return self; // we return self which might be nil
}
"The big, well-kept secret of Object-Oriented Programming is that it
is simple. It is based on a small number of common-sense fundamentals.
Unfortunately OOP has its own special terminology, & it suffers from
the 'surfeit of Zen' problem: to fully understand any 1 part, you need
to understand most of the other parts. Most programmers can understand
OOP instinctively if it is explained clearly." --- Peter van der Linden
1996 _Just Java_ pp 33-34
John G. Otto, Eagle Scout, Knight, Cybernetic Praxeologist
Existence, Consciousness, Identity, Life, Liberty, Property, Privacy, Justice
<
http://gosc.nisus.com/~giovanni>