Is it necessary to assign a string to a variable before comparing it to another?

后端 未结 4 1589
名媛妹妹
名媛妹妹 2020-11-30 20:21

I want to compare the value of an NSString to the string \"Wrong\". Here is my code:

NSString *wrongTxt = [[NSString alloc] initWithFormat:@\"W         


        
4条回答
  •  天命终不由人
    2020-11-30 21:24

    Brian, also worth throwing in here - the others are of course correct that you don't need to declare a string variable. However, next time you want to declare a string you don't need to do the following:

    NSString *myString = [[NSString alloc] initWithFormat:@"SomeText"];
    

    Although the above does work, it provides a retained NSString variable which you will then need to explicitly release after you've finished using it.

    Next time you want a string variable you can use the "@" symbol in a much more convenient way:

    NSString *myString = @"SomeText";
    

    This will be autoreleased when you've finished with it so you'll avoid memory leaks too...

    Hope that helps!

提交回复
热议问题