NSString self modifying category dilemma

Both work, but which one would you use and why?

@implementation NSString (Extender)

    -(NSString *) stringByTrimmingPrefix:(NSString *)strPrefix 
    {
        while ([self hasPrefix:strPrefix]) 
        {
            self = [self substringFromIndex:strPrefix.length];
        }
        return self;
    }

@end

or

@implementation NSString (Extender)

    -(NSString *) stringByTrimmingPrefix:(NSString *)strPrefix 
    {
        NSString *returnValue = [NSString stringWithString:self];
        while ([returnValue hasPrefix:strPrefix]) 
        {
            returnValue = [returnValue substringFromIndex:strPrefix.length];
        }
        return returnValue;
    }

@end

Answers


Option #2.

NSString is intended to be an immutable object. All of the standard "stringBy" methods on NSString return new autoreleased NSStrings.

While #1 also ends up returning a new NSString, it is at best semantically incorrect and at worst altering the referenced object of what was supposed to be an immutable object.


Need Your Help

Google maps fitBounds with padding?

javascript google-maps fitbounds

We want something like this leaflet fitBounds in our google maps application. We saw a few examples but it doesn't worked as expected. We tried to make the div as a google maps controller, extend the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.