• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSArray arrayWithObjects: count: method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSArray arrayWithObjects: count: method


  • Subject: Re: NSArray arrayWithObjects: count: method
  • From: Bill Bumgarner <email@hidden>
  • Date: Sun, 13 Apr 2003 13:46:13 -0400

On Sunday, Apr 13, 2003, at 09:23 US/Eastern, email@hidden wrote:
I would like to create an NSArray of objects, ten of the same type.

It looks like the

+ (id)arrayWithObjects:(id *)objs count:(unsigned)cnt;

method would be a good choice.

Given that you only need 10 objects -- and even if you needed 100-- I would suggest punting on trying to optimize the creation of the objects. It is extremely unlikely that the difference between using +arrayWithObjects:count: and simply adding the objects one at a time will make *any* difference in the overall performance of your app.

NSMutableArray *modules = [NSMutableArray array];
for(;[modules count]<10;)
[modules addObject: [[[MacdobModule alloc] init] autorelease]];

If you really want to go the route of using arrayWithObjects:count:, then:

id objects[10];
int i;

for (i=0; i<10; i++)
objects[i] = [[[MacdobModule alloc] init] autorelease];

NSArray *modules = [NSArray arrayWithObjects: objects count: 10];

Given that NSArray is going to have to loop across the array of objects and retain each anyway, the actual advantages associated with +arrayWithObjects:count: are less than you might assume.

If the -autorelease bugs you, then do:

NSMutableArray *modules = [NSMutableArray array];
for(;[modules count]<10;) {
MacdobModule *mod = [[MacdobModule alloc] init];
[modules addObject: mod];
[mod release];
}

And, as mmalcolm pointed out, you can convert to an immutable array via...

modules = [NSArray arrayWithArray: modules];

.... however, it is unlikely that there are any advantages to an immutable array vs. a mutable array for read-only access unless you have thousands of objects in the array. Even then, I don't know if mutable array's do anything like multiple internal arrays or not. More likely than not, both mutable and immutable arrays implement 'objectAtIndex:' as 'return objectArray[i];'.

b.bum

(Obviously, Dave may be asking a question where he really is creating 10s of thousands of objects in the array... the answer provided is assuming that that is not the case because such a case is exceedingly rare. Assuming that it isn't the case, then there is no point in trying to optimize the creation of the array at this time -- do the optimization AFTER it has been determined that there really is a performance issue.)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
  • Prev by Date: Re: Adding something to the login items
  • Next by Date: The release of NSWindowController
  • Previous by thread: Re: NSArray arrayWithObjects: count: method
  • Next by thread: Count files in directory
  • Index(es):
    • Date
    • Thread