@ "" compared to [NSString string]

I'm trying to figure out the pros and cons of @"" and [NSString string] , as they seem to behave the same way. I guess I hope that [NSString string] returns a static value, avoiding unnecessarily creating a string in memory. Can anyone shed some light, as the documentation is inconclusive.

+6
source share
6 answers

It is very likely that @"" == [NSString string] and therefore the solution is just a matter of style. Yeah, he's gone:

 NSLog(@"%i", @"" == @""); // 1 NSLog(@"%i", [NSString string] == [NSString string]); // 1 NSLog(@"%i", [[NSString string] stringByAppendingString:@"foo"] == [@"" stringByAppendingString:@"foo"]); // 1 NSLog(@"%i", @"" == [NSString string]); // 0 

But I can’t come up with any precedent where the difference will make a difference.

+5
source

@"" is a literal string and will exist at runtime. [NSString string] is probably a dynamically allocated object (but can be optimized so that it does not exist), and it could (in theory) be collected at runtime when it was no longer referenced. Both literal and dynamically distributed strings use sharing internally, so two @"" or two [NSString string] are likely to return the same object, even if not in the same file, but there is no guarantee that they will be. However, they cannot return the same object to each other.

In general, it does not matter, you should not rely on the equality of the pointer to check for equality of strings.

Looking at the assembly code, I would suggest that @"" has a performance advantage, but thinking that this is important, this is actually a case of premature optimization.

Just use @"" , it makes sense to be short and clear.

+2
source

I am sure that [NSString string] is just a convenience method for [[[NSString alloc] init] autorelease] , which is identical to @"" in semantics.

This means that there is no use for memory.

+1
source

@ "" is used to remove NSString. string is used to remove NSMutableString.

 NSString * string = @"String"; NSMutableString * mutablestring; [mutablestring setString:@"Mutable String"]; NSLog (@"%@", string); // String NSLog (@"%@", mutablestring); // Mutable String string = @""; mutablestring = [NSMutableString string]; NSLog (@"%@", string); // empty, nothing actually prints in NSLog NSLog (@"%@", mutablestring); // empty, nothing actually prints in NSLog //The below statement gives warning. mutablestring = @""; 
+1
source

@ "is a constant line, so the -retain, -release and -autorelease messages are ignored. It also cannot be freed.
[NSString string] is already auto-implemented.

0
source

Here I have only one thing: this is what I use, it is really an empty string. Every time I see a [NSString string] , I am guaranteed to get 100% a string with .length of 0 .

However, when I see @"" , I'm not sure if it is empty. Now 99.9999%, which is a safe bet, but it can be just as easily @"​" ( zero-width space ), backspace , zero-width without participation , etc. We hope that the sadistic coder who used this put a comment next to it, saying that this is a zero-width space, but so that my code is not so confused, I use [NSString string] .

0
source

Source: https://habr.com/ru/post/905692/


All Articles