Does Objective-C use a string pool?

I know that Java and C # use a string pool to save memory when working with string literals.

Does Objective-C use any such mechanism? If not, why not?

+4
source share
1 answer

Yes, string literals like @"Hello world" are never released, and they point to the same memory, which means that the pointer comparison is true.

 NSString *str1 = @"Hello world"; NSString *str2 = @"Hello world"; if (str1 == str2) // Is true. 

It also means that a weak string pointer will not change to nil (which happens for regular objects), since a string literal is never freed.

 __weak NSString *str = @"Hello world"; if (str == nil) // This is false, the str still points to the string literal 
+5
source

All Articles