Search code examples
iosswiftobjective-c

Difference between String in Swift and NSString in Foundation(Objective C)


What is the actual difference between String in Swift and NSString in Foundation(Objective C).

I have seen all functions of NSString can be applied to String in swift. For example componentsSeparatedBy is function of NSString but works with String also.

So what exactly is difference between them?

Tried running NSString functions on Strings.


Solution

  • For general advice on which to use and why, see Swift - which types to use? NSString or String. However, I believe you are aware of that, and this is a different question. What actually are the differences?

    The biggest difference that has the most impact is that NSString is uses UTF-16 as its preferred internal encoding and String prefers UTF-8. Both can encode things other ways, but that's their defaults, and this has a big impact on them. Since almost all modern work is done in UTF-8, String is more natural for this.

    While it's obvious that NSString is a class and String is a struct, it is less obvious that String has a SmallString storage which can store very short strings directly in two Ints avoiding heap memory allocations and reference counting entirely. This is the kind of optimization that is difficult to achieve with the design of NSString.

    NSString is fundamentally composed of UTF-16 code points. String is fundamentally composed of "extended grapheme clusters" (Character). This has major implications in how it works with Unicode. String is a much more "Unicode-aware" type than NSString. Alternately you can say it is a very "Unicode-pedantic" type. Between the use of UTF-8, which is variable width, and Character rather than code point as the element, finding the "nth" element of a String is a O(n) process. It's O(1) in NSString, but you then must do extra work to be sure you're not in the middle of a combined character.

    Ultimately while there is very handy bridging between the two types, they are very different, and String is much more powerful, especially when dealing with complex Unicode such as emoji.