官术网_书友最值得收藏!

Characters and strings

For some time in this chapter, we've been mentioning strings. Strings are also a collection of data types, but a specially dealt collection of characters, of the class type, string. Swift is Unicode-compliant, so we can have strings like the following:

let gameOverText =  "Game Over!"

We can have strings with emoji characters like the following:

let cardSuits =  "? ? ? ?"

What we did in the preceding code was create what's known as a string literal. A string literal is when we explicitly define a string around two quotes "".

We can create empty string variables for later use in our games such as:

var emptyString = ""               // empty string literal
var anotherEmptyString = String()  // using type initializer

Both are valid ways to create an empty string "".

String Interpolation

We can also create a string from a mixture of other data types, known as String Interpolation. String Interpolation is rather common in game development, debugging, and string use in general.

The most notable of examples are displaying the player's score and lives. This is how one of our example games, PikiPop, uses String Interpolation to display the current player stats:

//displays the player's current lives
var livesLabel = "x \(currentScene.player!.lives)"

//displays the player's current score
var scoreText = "Score: \(score)"

Take note of the \(variable_name) formatting. We've actually seen this before in our past code snippets. In the various print() outputs, we used this to display the variable, collection, and so on we wanted to get information on. In Swift, the way to output the value of a data type in a string is by using this formatting.

For those of us who came from Objective-C, it's the same as the following:

NSString *livesLabel = @"Lives: ";
int lives = 3;
NSString *livesText = [NSString stringWithFormat:@" %@ (%d days ago)", livesLabel, lives];

Note how Swift makes String Interpolation much cleaner and easier to read than its Objective-C predecessor.

Mutating strings

There are various ways to change strings, such as adding characters to a string as we did to collection objects. Here are some basic examples:

var gameText = "The player enters the stage"
gameText += " and quickly lost due to not leveling up"
/* gameText now says
"The player enters the stage and lost due to not leveling up" */

Since strings are essentially arrays of characters, like arrays, we can use the += assignment operator to add to the previous string.

Also, akin to arrays, we can use the append() function to add a character to the end of a string.

 let exclamationMark: Character = "!"
gameText.append(exclamationMark)
//gameText now says "The player enters the stage and lost due to not leveling up!"

Here's how we iterate through the characters in a string, in Swift:

for character in "Start!" {
    print(character)
}
//outputs:
//S
//t
//a
//r
//t
//!

Note how again we use the for-in loop and even have the flexibility of using a string literal if we'd so like to be what's iterated through by the loop.

String indices

Another similarity between arrays and strings is the fact that a string's individual characters can be located via indices. Unlike arrays, however, since a character can be a varying size of data, broken in 21-bit numbers known as Unicode scalars, they can not be located in Swift with Int type index values.

Instead, we can use the .startIndex and .endIndex properties of a string and move one place ahead or one place behind the index with the .successor() and .predecessor() functions, respectively, to retrieve the needed character or characters of a string.

Here are some examples that use these properties and functions using our previous gameText string:

gameText[gameText.startIndex]              // = T
gameText[gameText.endIndex]                // = !
gameText[gameText.startIndex.successor()]  // = h
gameText[gameText.endIndex.predecessor()]  // = p

Note

There are many ways to manipulate, mix, remove, and retrieve various aspects of strings and characters. For more information, be sure to check out the official Swift documentation on characters and strings at https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html.

主站蜘蛛池模板: 泸州市| 体育| 奇台县| 澄迈县| 汉中市| 莫力| 文化| 惠水县| 报价| 赣榆县| 临沂市| 古丈县| 邵阳县| 东明县| 微山县| 牟定县| 蓝田县| 东平县| 任丘市| 永寿县| 衡东县| 大同县| 舟曲县| 宁南县| 兴国县| 沾化县| 呼和浩特市| 南乐县| 宁陕县| 广东省| 景宁| 进贤县| 呈贡县| 黎城县| 凤阳县| 桑植县| 灯塔市| 内丘县| 娱乐| 温宿县| 新化县|