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

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.

主站蜘蛛池模板: 延川县| 高要市| 台东县| 新巴尔虎右旗| 林州市| 和林格尔县| 黔南| 青阳县| 佛坪县| 盖州市| 北流市| 河西区| 兴仁县| 江城| 泰兴市| 巴中市| 阆中市| 页游| 洞头县| 河南省| 吴忠市| 班玛县| 松桃| 巴彦淖尔市| 进贤县| 道孚县| 富民县| 红河县| 巫山县| 芦山县| 光山县| 绥德县| 钟山县| 襄城县| 宿松县| 吴江市| 环江| 琼结县| 绥江县| 浮梁县| 香格里拉县|