i've noticed that all @"" objects create one reference for all times it is executed.
NSString *s1=@"";
NSString *s2=@"";
In this sample s1 equals s2.
@"" will create one pointer in all cases, every time i use it?
Can i rely on this feature in comparing strings in objective-c?
Or simply, can i use this statement, if i want to assure that my string is empty:
if(s == @""){
//do something
}
Yes Objective C has an optimization in the compiler that simply points all equivalent string literals to the same string in memory to avoid allocating unnecessary resources. This feature is reliable but there is a chance that this will not always happen as documented in the Objective C language specs.
you should use
if([s isEqualToString:@""])
来源:https://stackoverflow.com/questions/4693665/does-xcode-creates-one-object-for-all-empty-strings