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

别等时光非礼了梦想. 提交于 2019-11-28 08:31:50

问题


This question already has an answer here:

  • Obj-C: [NSString stringWithString:@“string”] vs. @“string” 3 answers

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"


回答1:


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"




回答2:


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.




回答3:


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

statusString = @"";

instead of

statusString = [NSString stringWithString:@""];



回答4:


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.



来源:https://stackoverflow.com/questions/13373826/using-stringwithstring-with-a-literal-is-redundant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!