let answer = shouldBeTuple ? Tuple : Struct/Class

Table of Content

If you’re new to Swift (or programming in general), or even if you’re not, you might be wondering why you would use a tuple over a class or struct. Here’s a simple guideline:

What would tuples do?

Tuples don’t offer you autocomplete assistance. For example, a tuple with a String and Int would simply be created like

let myNewTuple = ("I am a string.", 42)
// random note: while it is equally valid to create a tuple like this, 
// myNewTyple and mySecondNewTuple are NOT identical, 
// despite containing the same data
let mySecondNewTuple = (42, "I am a string.")

However, when accessing their properties, autocomplete DOES kick in!

let myNewTuple = ("string ring ding aling", 42)
myNewTuple.0 // "string ring ding aling"
myNewTuple.1 // Int: 42

You can even name the indicies in a tuple to be able to access them without having to remember what an index correlates to:

let foo = (phrase: "Wheel of Fortune", winnings: 45_000)
foo.phrase // "Wheel of Fortune"
foo.winnings // Int: 45000

The advantage is they are super quick and easy to create. Their disadvantage is that you have to define not just the contents, but what they are EVERY time you create one. Without autocomplete assistance.

Teach me how to Structy/Classy

(I’m only going to focus on Struct specifics, but most/all of this applies to Classes too.)

Contrarily Structs DO off autocomplete assistance, but require that you define exactly what they are comprised of in advance. Basically, before you do

let myNewStruct = GameShow(phrase: "Axel of Disparity", winnings: 2)

you first need to define what GameShow is. This can either be in the same file (typically discouraged) or in its own file (typically encouraged).

struct GameShow {
    let phrase: String
    let winnings: Int

And like I said, autocomplete will help you out as soon as you get to the G in let myNewStruct = G, which makes filling out your data quicker and easier. Additionally, you can also decide to create an initializer that has as many or as little parameters as you’d like. Maybe winnings will typically start at 0, so it would make sense to want to do this:

struct GameShow {
    let phrase: String
    let winnings: Int

    init(phrase: String) {
        self.phrase = phrase
        self.winnings = 0

let myNewStruct = GameShow(phrase: "Axel of Disparity")
myNewStruct.winnings // Int: 0 (even though we didn't explicitly set it in the previous line)

Why is Autocomplete so Important?

It may not seem like a huge deal, but when you are creating and passing this data around in several areas of your app, it can be a huge pain to have to write out its properties every time. In contrast, if you have a function that provides two pieces of information that just happen to be associated in this one particular instance, it is overkill to create a whole struct JUST to be able to pass two (or more) pieces of info around your app just once.

The Absolute 100% Always Do It This Way And Never Waver From This Path

The title is opinionated. You don’t have to do it like this, but it’s how I do it, so it’s naturally 100% right.

It’s actually quite simple. Just ask yourself these questions:

Do I need to do use this set of data in several places? Do I want autocomplete assistance when I create a new copy of this kind of data?

If the answer is yes to either of these questions, I’d lean heavily toward a Struct/Class. If not, tuples are a nice and quick way to move data around!

Leave a Reply

Your email address will not be published. Required fields are marked *