-2

I wonder why I am getting different results in each of the following lines

let spain = "España"
print (spain.count) -- return 6
print (spain.utf8.count) -- return 7
print (spain.utf16.count) -- return 6
print (spain.utf8CString.count) -- return 8
casillas
  • 16,351
  • 19
  • 115
  • 215
  • 3
    Why do you expect them to be the same? They are different encodings. – rmaddy Dec 15 '18 at 04:08
  • 1
    Have a look at "Unicode Representations of Strings" in https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html – Martin R Dec 15 '18 at 04:08
  • Not sure what you're using the count for, but if it's for something like figuring out a range, you may find this helpful: https://stackoverflow.com/a/35193481/4475605 – Adrian Dec 15 '18 at 05:23

1 Answers1

1

The count in String and in String.UTF8View, String.UTF16View etc are different because their way of storage is different although they are all collections. String stores them in a collection of characters, so each item in the collection represents one character. However, in other encodings, they are stored in a collection of that encoding's code units. Some special characters (like ñ) have to be stored in two units. For ñ, it is 0xC3 0xB1 in UTF8 while it is 0x00F1 in UTF16, so you can see that there is one more item in the UTF8 encoding than UTF16. You cannot get the number of characters accurately with count unless you are using the base String.

Papershine
  • 4,995
  • 2
  • 24
  • 48