Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
iOS 9 Game development Essentials

You're reading from   iOS 9 Game development Essentials Design, build, and publish an iOS game from scratch using the stunning features of iOS 9

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781784391430
Length 224 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Chuck Gaffney Chuck Gaffney
Author Profile Icon Chuck Gaffney
Chuck Gaffney
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. The Swift Programming Language 2. Structuring and Planning a Game Using iOS 9 Storyboards and Segues FREE CHAPTER 3. SpriteKit and 2D Game Design 4. SceneKit and 3D Game Design 5. GameplayKit 6. Exhibit the Metal in Your Game 7. Publishing Our iOS Game 8. The Future of iOS Game Development Index

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime