Re: substring return a new string?
Re: substring return a new string?
- Subject: Re: substring return a new string?
- From: Pete Yandell <email@hidden>
- Date: Fri, 19 Mar 2004 10:47:48 +1100
On Thursday, Mar 18, 2004, at 13:16 Europe/Prague, Marco Scheurer wrote:
This is often not the case in practice. It is a common idiom, for
methods returning containers such as NSArray or NSDictionary, but also
NSString or NSData to return a mutable one which is created and filled
in the method:
- (NSArray *) someArray
{
NSMutableArray *someArray = [NSMutableArray array];
// some logic and [someArray addObject:anObject];
return someArray;
}
I've always thought that there should be some way to turn a mutable
array into a non-mutable array for exactly this case, given that it
comes up so often. You'd have to construct a new NSArray object of
course (different classes and all that), but you could potentially grab
a reference to the contents straight out of the NSMutableArray and
destroy it in the process - or at least empty it of its contents - as
opposed to actually copying the contents. (I'm assuming here that
NSArray and NSMutableArray store their data internally in the same
format.)
So you'd have something like:
- (NSArray*)someArray
{
NSMutableArray* someArray = [NSMutableArray array];
// some logic and [someArray addObject:anObject];
return [NSArray arrayByStealingTheContentsOfArray:someArray];
// This would leave someArray empty, but that's OK given it's being
destroyed anyway.
// A better method name is probably in order here too. :)
}
Then you could safely use [array copy] in all those places that [array
retain] currently gets used and probably shouldn't be.
Pete Yandell
http://pete.yandell.com/
_______________________________________________
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.