Using 'stringWithString:' with a literal is redundant [duplicate]
Asked Answered
B

4

5

i used this code in reachability class that is in ios6

   switch (status) {
        case kNotReachable:
            statusString = [NSString stringWithString: @"Not Reachable"];
            break;
        case kReachableViaWWAN:
            statusString = [NSString stringWithString: @"Reachable via WWAN"];
            break;
        case kReachableViaWiFi:
            statusString = [NSString stringWithString: @"Reachable via WiFi"];
            break;
    }

but the following error is occurred "Using 'stringWithString:' with a literal is redundant"

Bluepencil answered 14/11, 2012 at 6:4 Comment(2)
Why not just use the literals by themselves? Rather than using stringWithStringTheocracy
why don't you use statusString = @"Not Reachable"; ?Grum
I
9

The warning is saying that you could instead easily do like this:

statusString = @"Not Reachable";

The explanation is provided in the post Obj-C: [NSString stringWithString:@"string"] vs. @"string"

Ingenerate answered 14/11, 2012 at 6:11 Comment(0)
D
4

Instead of using

statusString = [NSString stringWithString: @"Not Reachable"];

please write your code like below:

statusString = @"Content-Type: Not Reachable/unknown\r\n\r\n";

warning will be removed.

Drone answered 20/12, 2012 at 6:58 Comment(0)
B
2

You resolve these 'warnings' simply by declaring your strings like so:

statusString = @"";

instead of

statusString = [NSString stringWithString:@""];
Brahui answered 14/11, 2012 at 6:11 Comment(0)
U
0

I think this is a type of compiler optimization. Actually you need to assign a string to variable. You can do it directly as myString = @"" no need to call a method and it will use additional processing time.

Uella answered 20/12, 2012 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.