Swift notes string

  • 2020-05-10 22:59:39
  • OfStack

string

Introduction to the

Strings in String are value types that are copied when passed, while strings in NSString are references. We can traverse the string with for in:


var a : String = "a"
for c in "Hello" {
    println(c)
}

You can count the number of characters in a string by countElements:


countElements("1234567") // 7

Note, however, that countElements and NSString's length are not always exactly the same value, because length makes use of UTF-16 instead of Unicode characters. For example, when the emoji expression is added, UTF-16 calculates 2, while Unicode calculates 1. Here's an example:


var a = "Hello

That's all for this article, I hope you enjoy it.


Related articles: