Re: Using NSMutableString* for NSString* result
Re: Using NSMutableString* for NSString* result
- Subject: Re: Using NSMutableString* for NSString* result
- From: Nicholas Riley <email@hidden>
- Date: Fri, 3 Oct 2003 11:41:00 -0500
- Mail-followup-to: Matt Gough <email@hidden>, cocoa dev <email@hidden>
On Fri, Oct 03, 2003 at 04:28:54PM +0100, Matt Gough wrote:
>
Is it OK to just return (NSString*)myMutableString, or do I have to do copy
>
the string in to a 'proper' NSString?
There is actually no such thing as a concrete NSString. NSString is a
class cluster; you're most likely to be actually dealing with
instances of private classes such as NSCFString and the like.
For any method which you declare as responding with an instance of a
particular class, it's fine to respond with any instance of any
subclass.
>
(It is assumed that the function has no knowledge as to how its
>
result will be used elsewhere)
If that NSMutableString you're returning is only treated as an
NSString by the caller, then you should be fine. Since Objective-C
doesn't attempt to enforce any safety in any case, it's usually not
worth the efficiency cost to be so careful about exposing internal
structures. Cocoa does the same thing; you never have to know about
the identities of the private classes you're using.
>
i.e. given the two versions below, which is considered more correct by those
>
of you with more experience of this sort of stuff?
>
>
>
Example 1:
>
-(NSString*) getSomeFancyString
>
{
>
NSMutableString* s = [NSMutableString new];
You don't want to use 'new'; use 'alloc', 'init'. Please (re)read
Apple's Objective-C book.
>
[s appendString:getSomeOtherString()];
>
... Other stuff to add/change the string
You should use either:
NSMutableString *s = [[NSMutableString alloc] initWithString: getSomeOtherString()];
or:
NSMutableString *s = [getSomeOtherString() mutableCopy];
assuming 'getSomeOtherString()' returns a NSString.
>
return (NSString*)[s autorelease];
No need to cast here, NSMutableString is a subclass of NSString.
>
-(NSString*) getSomeFancyString
>
{
>
NSMutableString* s = [NSMutableString new];
>
[s appendString:getSomeOtherString()];
>
... Other stuff to add/change the string
>
>
NSString* result = [NSString stringWithString:s];
>
>
[s release];
Instead of the above (for which you did get retain/release behavior
correct! :) you can also use:
NSString *result = [[s copy] autorelease];
which while equivalent to +[NSString stringWithString:] I usually find
easier to understand.
I'd typically do the second thing, because I assume (perhaps
incorrectly) in general that an immutable string would be smaller than
a mutable string of the same length. For a huge string, you may be
better off not copying the buffer. Or for all I know, the -copy
method may share the buffer...
--
=Nicholas Riley <email@hidden> | <
http://www.uiuc.edu/ph/www/njriley>
_______________________________________________
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.