Is it ever possible to create a reference to element of array of struct?

Viewed 156

Given an Array with struct

    import Foundation
    
    struct Card {
        var flag: String = ""
    }
    
    var cards = Array<Card>()
    cards.append(Card())

The following operation will NOT modify original array element

    // A copy is created.
    var cardCopy = cards[0]
    
    // Will NOT modify cards[0] 
    cardCopy.flag = "modify0"
    
    print(cards[0].flag)

The following operation will modify original array element

    // We can modify cards[0] by
    cards[0].flag = "modify"

    print(cards[0].flag)

However, it isn't efficient in the sense, we need to perform indexing access each time. Imagine

    cards[0].flag0 = "modify"
    cards[0].flag1 = "modify"
    cards[0].flag2 = "modify"
    cards[0].flag3 = "modify"
    ...

Is there a way, we can create reference to element of array of struct? So that we can write

// How to create a reference to cards[0]?
var cardReference = ...
    cardReference.flag0 = "modify"
    cardReference.flag1 = "modify"
    cardReference.flag2 = "modify"
    cardReference.flag3 = "modify"
    ...

One of the possibilities is to replace struct with class. But, I would like to explore other alternative, before doing so.

2 Answers

You can achieve that using a function to make your changes and pass the Card struct by reference like this:

func update(card: inout Card) {
    card.flag0 = "modify"
    card.flag1 = "modify"
    card.flag2 = "modify"
    card.flag3 = "modify"
}

var cards = Array<Card>()
cards.append(Card())

update(card: &cards[0])

or even better by using a mutating function in Card type and pass as a closure your changes like that:

struct Card {
    var flag0: String = ""
    var flag1: String = ""
    var flag2: String = ""
    var flag3: String = ""
    
    mutating func update(block: (inout Card) -> Void) {
        block(&self)
    }
}
    
var cards = Array<Card>()
cards.append(Card())

cards[0].update {
    $0.flag0 = "modify"
    $0.flag1 = "modify"
    $0.flag2 = "modify"
    $0.flag3 = "modify"
}

Update: To make the second approach even more reusable, you can define a protocol like this:

protocol Updatable {
    mutating func update(block: (inout Self) -> Void)
}

extension Updatable {
    mutating func update(block: (inout Self) -> Void) {
        block(&self)
    }
}

and make Card struct conform to it:

struct Card: Updatable {
    var flag0: String = ""
    var flag1: String = ""
    var flag2: String = ""
    var flag3: String = ""
}

Then you can use it just like above.

This is the expected behaviour, since Array is a struct and structs are value types.

What you need is reference type behaviour, so you should convert your struct to a class.

Another solution for modifying several properties of a value type in one go is to create a mutating function that does that and calling that on the array element.

struct Card {
    var flag: String = ""
    var flag2 = ""

    mutating func update(flag: String, flag2: String) {
        self.flag = flag
        self.flag2 = flag2
    }
}

var cards = [Card(flag: "a", flag2: "b")]
cards[0].update(flag: "b", flag2: "c")
Related