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.
inoutis as close as you're going to get.