Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

I was working on a small prototype for a grid-based game yesterday, and I ran into a classic architectural annoyance. I needed a way to track a player's position on a 2D map. My first instinct was to just use two separate variables, playerX and playerY. But as soon as I started writing a function to move the player, I realized I was passing around two different integers every single time.

The "Too Many Arguments" Problem

Let's look at how that looks in code. I tried this initially:

func movePlayer(x: Int, y: Int) {
    print("Moving to \(x), \(y)")
}

movePlayer(x: 10, y: 20)

It works, sure. But if I want to store that position or pass it to another function—say, a function that checks if the player hit a wall—I'm stuck managing these two separate values everywhere. Now, I could build a whole struct for a Point, but for a quick prototype, that feels like overkill. I don't need a full type definition; I just need these two numbers to stick together.

Grouping without the Ceremony

This is where I remembered tuples. A tuple is basically a way to group a few values together into a single compound value without having to define a formal structure. I decided to rewrite the position as a single variable:

var playerPosition = (10, 20)
print("The player is at \(playerPosition)")

That's it. No struct, no class. Just parentheses. But then I tried to use the values inside the tuple, and it felt... a bit robotic.

print("X coordinate: \(playerPosition.0)")
print("Y coordinate: \(playerPosition.1)")

Using .0 and .1 is technically correct, but it's a nightmare for readability. If I come back to this code in a week, I might forget if .0 was the X or the Y coordinate. It's a common trap in Swift—relying on index-based access for tuples makes your code brittle.

Naming the Pieces

I realized I could make the tuple much more expressive by giving the elements names. I adjusted the declaration like this:

var playerPosition = (x: 10, y: 20)
print("X coordinate: \(playerPosition.x)")
print("Y coordinate: \(playerPosition.y)")

Now it reads like a sentence. I'm still using a tuple, but I've added a layer of documentation directly into the variable. This is particularly powerful when you're returning multiple values from a function. Usually, a function can only return one thing. But what if I want a function to return both the player's new position and a boolean indicating if they hit an obstacle?

func updatePosition(current: (x: Int, y: Int), dx: Int, dy: Int) -> (newPos: (x: Int, y: Int), hitWall: Bool) {
    let nextX = current.x + dx
    let nextY = current.y + dy
    
    let hitWall = (nextX > 100 || nextY > 100) // Simplified wall logic
    return ((nextX, nextY), hitWall)
}

let result = updatePosition(current: playerPosition, dx: 5, dy: -2)
print("New position: \(result.newPos), Hit wall: \(result.hitWall)")

Cleaning up the Extraction

The result.newPos syntax is fine, but it's still a bit verbose. I noticed that Swift lets me "decompose" or "destructure" a tuple into separate constants. I tried this instead of using the result variable:

let (finalPos, collided) = updatePosition(current: playerPosition, dx: 5, dy: -2)
print("Position is now \(finalPos) and collision is \(collided)")

I love this pattern. It allows me to grab exactly the pieces of data I care about and give them clean, local names immediately. It keeps the logic flow lean and avoids that repetitive result.something chain.




📋 Practical Task

Build a Weather Snapshot Tool

You are building a weather app. Instead of creating a full struct for a single reading, you want to use a tuple to handle the data for a specific city.

Your task:

  • Create a function called getWeatherReading that takes a city: String as an argument.
  • The function should return a named tuple containing three elements: temperature (Double), humidity (Int), and condition (String).
  • Inside the function, you can return hardcoded values (e.g., 22.5, 60, "Sunny").
  • In your main code, call this function and use tuple destructuring to assign the temperature, humidity, and condition to three separate constants.
  • Finally, print a message using those constants, such as: "In London, it is 22.5 degrees with 60% humidity and Sunny."
Rating
0 0

There are no comments for now.

to be the first to leave a comment.