3 ms·
> Objective-C/Cocoa has NSMutableString and NSString. This (or value typed strings) is an important optimization that most other systems don't properly have. I
by astrange 4y ago
> Objective-C/Cocoa has NSMutableString and NSString.
This (or value typed strings) is an important optimization that most other systems don't properly have. If you know the string is immutable, so it won't ever have to be resized, you can do a single allocation (string+buffer) instead of having an extra pointer dereference (string->buffer).
- afdbcreid 4y agoI don't know Objective-C/Cocoa, but is it when you know the size at compile time? If yes, you can use an array.
- astrange 4y ago“At compile time” is too strict; that’s a constant string. And an array isn’t a string, it’s an array. (A string is only a byte array because everything is a byte array.)
- ratmice 4y agoNo it is when you know the size during runtime, or at time of construction. there is a 3rd class for constant strings known at compile time but it is mostly hidden behind @"" compiler syntax.
- astrange 4y agoNS(Mutable)Strings aren’t “classes” but “class clusters”. Making them classes would be an inappropriate mix of interface and implementation. Of course, despite it being a bad idea, almost all other data structure library does do it that way. Constant strings have the same memory layout as other strings more-or-less but have different layouts for ASCII and Unicode, and in memory immutable strings have different backing classes for tagged pointer storage and for UNIX paths.