Samarth Upadhyaya
Samarth Upadhyaya

Reputation: 31

Strange way of declaring an array in swift?

I was watching lecture 2 in Stanford's iOS development course and noticed an odd way of declaring an array.

var cards: Array<MemoryGame<String>.Card> {
    return model.cards
}

I do not recognize this method of initializing an array and have not been able to find any details about it in Apple's documentation. When I tried to initialize the above array by using an equals sign and a closure, it stated that "Instance member 'model' cannot be used on type 'EmojiMemoryGame'; did you mean to use a value of this type instead?" I understand that this is because of the circular definition of the property cards and an instance of the class. However, I don't understand how the code above circumvents this issue. I also noticed that the code also correctly intialized the array in the sample case I set up below:

var arr1: Array<Int> = [1, 2, 3, 4, 5]
var arr2: Array<Int> {
    return arr1
}

Could someone please explain how this method of initializing arrays work or link me to a piece of documentation that explains this?

Upvotes: 0

Views: 54

Answers (1)

Gereon
Gereon

Reputation: 17844

this method of initializing an array

That's not what this is. This is a "computed property", explained here

Upvotes: 6

Related Questions