5

While working on project code left to me by a previous dev, I have encountered the following construct

-(NSString *)StringCheckWithString:(NSString *)string{

    NSString *string2 = [NSString stringWithFormat:@"%@", string];

    if([string2 length] == 0){
        return @"none";
    }
    else {
        return string2;
    }
}

Can anyone explain why you would do this, it seems significantly overengineered to me and I don't understand why it has been done this way (for clarity, I don't understand why the string is formatted like this, I understand the length check)

11
  • 3
    It's possible string is an unknown input. If it contains format characters and you use it raw, BAD THINGS can happen. Commented Jul 23, 2013 at 15:23
  • 2
    @thegrinner surely in the case string contains format characters, string2 now also contains those characters? Commented Jul 23, 2013 at 15:28
  • 2
    Hmmm.... Why was my comment deleted??? I pointed out that the effect of [NSString stringWithFormat:@"%@", string] (if it's really needed) can be achieved much more cheaply with [string description]. Though you far too often see the stringWithFormat thing used where it's entirely unnecessary -- it's like bad DNA that gets copied form one generation to the next. Commented Jul 23, 2013 at 15:49
  • 2
    Sorry, yes, that's what I meant. I'm guessing someone saw the explanation for why showing raw input in NSLog (ie NSLog(string) vs NSLog(@"%@", string)) was bad and assumed this would fix it in the general case. Commented Jul 23, 2013 at 15:53
  • 2
    @thegrinner -- I've seen this idiom far too often, in different code from different people. It seems to be mainly due to a mistaken belief that certain values (such as label text) must always be "formatted", and, once ensconced in someones code it tends to spread to other methods, then get copied by others. Commented Jul 23, 2013 at 16:10

1 Answer 1

8

The argument that is passed in could be any subclass of string, including NSMutableString. This code creates an immutable copy of it. This means that you can store the returned string without having to worry about someone else modifying it.

A better way of doing this would be:

NSString *string2 = [string copy];

According to the NSCopying Protocol reference:

The copy returned is immutable if the consideration “immutable vs. mutable” applies to the receiving object.

Sign up to request clarification or add additional context in comments.

10 Comments

I like this answer. From examining the usage, I doubt it was the intent, but it does make sense as a desirable effect
I agree with @jszumski - if I were going to write something to that effect, I would use copy.
doesn't [string copy] return an Object of the same class as string, while the intent here is to only ever return an immutable string?
@James, no, -copy always returns the immutable variant of the class (if it has one). Only -mutableCopy will return the mutable variant.
Well TIL. I did browse the NSString reference and couldn't see any mention of this behaviour, but the NSCopying reference is very clear. I've added a link to my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.