Re: WTF? How can this work?
Re: WTF? How can this work?
- Subject: Re: WTF? How can this work?
- From: Rainer Brockerhoff <email@hidden>
- Date: Mon, 20 Aug 2001 11:43:20 -0300
At 18:34 -0700 on 19/08/2001, Chris Kane wrote:
>
>> In 10.1, the memory "loss" and immutability [of NS collections] are recovered.
>
>
>
>Chris, could you expand that last sentence a little more? How are they "recovered"?
>
>
Instead of Foundation creating a CFMutableArray for all NSArrays, a CFArray is created for allocated NSArrays and a CFMutableArray is created for NSMutableArrays. Since the implementation for CF came right from NS (and has additionally been improved upon as recently discussed, as most mutable arrays are now managed as deques), the memory usage is now within a small constant amount plus or minus what the "old NS" implementation used.
Thanks! That's good news.
>
>I'm relieved to read your reply on the mutable question. Had you said the implementations were different, I'd be asking you now - rather plaintively - about the "secret method" Cocoa uses internally to construct immutable non-empty NSArrays.
>
>
I don't understand. There's no secret -- use -[NSArray initWithObjects:count:] or one of the other methods to get an immutable non-empty NSArray. We use methods like -initWithObjects:count: too internally. No magic.
You're right, I confess I was thinking of the general case, where I don't know beforehand how many members the array will have, so I have to add objects an NSMutableArray. In that case, as I said, I'm relieved there's no magic procedure to convert this to an immutable array.
>
>As a side-note, this is one of the reasons we should always declare object types instead of "id"s wherever possible - so the compiler can complain that addObject: shouldn't be used here, and so forth.
>
>
This doesn't really make a difference. The return type only helps if you're nesting like:
>
>
[[[NSArray alloc] initWith...] addObject: myObj];
>
>
but now you don't have a reference to the new array! In the usual pattern:
>
>
NSArray *newArray = [[NSArray alloc] initWith...];
>
[newArray addObject: myObj];
>
>
it's the declaration of newArray that makes all the difference. Declare it id instead, and no warning.
My point exactly. I always avoid declaring id in the circumstances of your second example.
Thanks again for your explanation.
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
http://www.brockerhoff.net/ (updated July 2000)