Using properties and methods

Let’s say you’re creating a game of cards and you want to store the players’ names in an array. The list will need to change as players join or leave the game, so you need to declare a mutable array:

var players = ["Alice", "Bob", "Cindy", "Dan"]

In this example, players is a mutable array because you assigned it to a variable.

Before the game starts, you need to make sure there are enough players. You can use the isEmpty property to check if there’s at least one player:

print(players.isEmpty)

// > false

Note: You’ll learn all about properties in Chapter 12, “Properties”. For now, just think of them as variables that are built in to values. To access a property, place a dot after the name of the constant or variable that holds the value and follow it by the name of the property you want to access.

The array isn’t empty, but you need at least two players to start a game. You can get the number of players using the count property:

if players.count < 2 {

print("We need at least two players!")

} else {

print("Let's start!")

}

// > Let's start!

It’s time to start the game! You decide that the order of play is by the order of names in the array. How would you get the first player’s name?

Arrays provide the first property to fetch the first object of an array:

var currentPlayer = players.first

Printing the value of currentPlayer reveals something interesting:

print(currentPlayer)

// > Optional("Alice")

The property first actually returns an optional, because if the array were empty,

first would return nil.

Similarly, arrays have a last property that returns the last value in an array, or nil

if the array is empty:

print(players.last)

// > Optional("Dan")

Another way to get values from an array is by calling min(). This method returns the element with the lowest value in the array — not the lowest index! If the array contains strings, then it returns the string that’s the lowest in alphabetical order, which in this case is "Alice":

currentPlayer = players.min() print(currentPlayer)

// > Optional("Alice")

Note: You’ll learn all about methods in Chapter 13, “Methods”. For now, just think of them as functions that are built in to values. To call a method, place a dot after the name of the constant or variable that holds the value and follow it by the name of the method you want to call. Just like with functions, don’t forget to include the parameter list, even if it’s empty, when calling a method.

Obviously, first and min() will not always return the same value. For example:

print([2, 3, 1].first)

// > Optional(2)

print([2, 3, 1].min())

// > Optional(1)

As you might have guessed, arrays also have a max() method.

Note: The first and last properties and the min() and max() methods aren’t unique to arrays. Every collection type has these properties and methods, in addition to a plethora of others. You’ll learn more about this behavior when you read about protocols in Chapter 19.

Now that you’ve figured out how to get the first player, you’ll announce who that player is:

if let currentPlayer = currentPlayer { print("(currentPlayer) will start")

}

// > Alice will start

You use if let to unwrap the optional you got back from first; otherwise, the statement would print Optional("Alice") will start and that’s probably not what you want.

These properties and methods are helpful if you want to get the first, last, minimum or maximum elements. What if the element you want can’t be obtained with one of these properties or methods?

results matching ""

    No results matching ""