Storing Text in Strings

Strings—blocks of textual data—are another essential data type supported in nearly all programming languages, and Swift’s are really great. Let’s start a new playground to try them out: create the playground with File > New > Playground, and call it StringsPlayground.

Of course, right off the bat, the playground template creates a string for us with var str = "Hello, playground", which evaluates as Hello, playground in the results pane.

Since str is defined with the var keyword, it’s a variable, so let’s change it up a bit. Type the following:

 str = str + ​"!!"

This evaluates as Hello, playground!!, and proves that we can combine strings with the + operator.

Swift strings are fully Unicode compliant, meaning they can contain any Unicode character, including all the various written languages and symbols supported by iOS. Let’s add some of those characters now. Xcode offers quick access to Unicode characters with the menu item Edit > Emoji & Symbols (space). The popover, shown in the following figure, allows quick selection of groups of emojis and other characters like “technical symbols” and “pictographs.” Scroll to the top to find a search field to look up characters by name, and a button to switch to the full-size character input window.

images/startingswift/emoji-and-symbols.png

Let’s see if we can add an emoji character to our string. Write the following code, and when you need to insert the emoji inside the quotes, bring up the symbols window, find an emoji character, and double-click it to insert it into the source code:

images/startingswift/string-plus-equals-running-person-emoji.png

As you’ll see in the results pane, this appends the emoji to the end of the string. And it turns out Swift’s support for Unicode isn’t just for strings; Unicode is fully supported throughout Swift source code as well. That means we can do something really silly, like this:

images/startingswift/running-person-equals-str.png

This creates a variable whose name is actually the running person emoji, and the assignment operator (=) sets its value to the current value of str, so we see the same value as before in the results pane.

Aside from the + operator, we can also build up strings through a substitution technique. Whenever the sequence () is found in a string in source code, the contents of the parentheses are evaluated and substituted into the string. The contents could be variables, mathematical expressions, or other strings, as in the following screenshot:

images/startingswift/book-and-phone-emoji-substitution.png

Now what about the contents of a string? In some languages, such as C, a string is just an array of characters. That’s largely true of Swift, subject to some technical details. Swift strings are really smart about Unicode, and sometimes multiple characters can be combined into one. Consider what happens when we combine Unicode’s “combining accent character” (Unicode code point 301) with the letter “e,” just as if we had typed e and then e:

 let​ accentedE = ​"e"​ + ​"​​u{301}​​"

This evaluates to the single character “é.” Two characters go in, and one comes out.

When we do want to pull out the contents of a string, we can use its characters property to let us treat the string contents as an array, one of the collection types we’ll be talking about in the next section. This lets us count the number of user-readable characters, and for kicks, we’ll substitute that number into a larger string:

 "Sentence has ​​(​sentence.characters.count​)​​ characters"

This evaluates to “Sentence has 25 characters” in the right pane.

The contents of the characters array are of a type called Character, which represents a single human-readable character. Using our Unicode string from before, we can find the location in the string of the “book” emoji by representing it as a Character, and then using the array’s indexOf to find it in the characters array.

images/startingswift/index-of-book.png

Once we write this, we see the result "book is at index Optional(10) in sentence". The 10 is right, but the Optional() is weird, right? We’re going to discover what’s up with that a little later.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.188