• 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: substring return a new string?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: substring return a new string?


  • Subject: Re: substring return a new string?
  • From: Ondra Cada <email@hidden>
  • Date: Thu, 18 Mar 2004 14:05:03 +0100

Marco,

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;
}

then what happens if you copy the returned array, is of course a real copy instead of a retain.

(unless someone uses mutable objects for immutable data, which, of course, is a design bug).

The above code is not a design bug, it is the only way to construct a non trivial, constant, array. You could return [NSArray arrayWithArray:someArray] or [[someArray copy] autorelease] instead, but then this amounts to do a copy each time the method is executed: very often unnecessary and inefficient.

Well... I would beg to differ a bit. IMHO, this design is truly oft-used, but (not to be considered a bug) only in cases where efficiency is not extremely important.

Presumed the speed/space is paramount, especially in contexts where it is probable the accessor may be used often and by more different modules, the logic -- in my personal opinion -- should generally (* see below) change to

@interface ... {
NSArray *arrayCache;
}
...
@implementation ...
...
-(NSArray*)someArray {
if (!arrayCache) {
NSMutableArray *someArray = [NSMutableArray array];
// some logic and [someArray addObject:anObject];
arrayCache=[someArray copy];
}
return arrayCache;
}

including of course the logic which would release&nil the arrayCache whenever the data change so that the aray result would too, and in dealloc.

(*) Of course, there are situations in which this approach is bad; my estimation though is that in a vast majority of cases (of those where efficiency is important) it's better than the plain returning the mutable array without saying so. Nevertheless, if the speed/space are very important and this approach seems wrong, so far as I can say the only remaining possibility is to *declare* the method returning mutable data, and *document* the fact it is always freshly-created: let then the client decide what to do with it:

@interface ...
...
-(NSMutableArray*)someArray; // created each time the message is sent, not stored then
...
// implementation exactly as you suggested.

---
Ondra Hada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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.


  • Follow-Ups:
    • Re: substring return a new string?
      • From: Pete Yandell <email@hidden>
    • Re: substring return a new string?
      • From: Marco Scheurer <email@hidden>
References: 
 >Re: substring return a new string? (From: Marco Scheurer <email@hidden>)

  • Prev by Date: Re: substring return a new string?
  • Next by Date: NSTask with complexe command
  • Previous by thread: Re: substring return a new string?
  • Next by thread: Re: substring return a new string?
  • Index(es):
    • Date
    • Thread