Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Joshua McMahon
Joshua McMahon
6,778 Points

Issues iterating over an Array

I'm having issues when attempting to iterate over arrays that contain structs. All of the code below works, and builds me a deck of cards (called newDeck) that consists of different card properties (color, shade, count, and shade).

The issue arises when I try and iterate over the array of cards and attempt to print each one out in the console, just to see if they all came out okay. For whatever reason, the compiler will not let me iterate over the array. I could hard code it like the print command loop, but I know this is bad code and I just want to understand how to set up my arrays so I can easily iterate over them.

let color: Set = ["red", "green", "purple"]
let shape: Set = ["oval", "diamond", "squiggle"]
let count: Set = [1, 2, 3]
let shade: Set = ["empty", "shaded", "fill"]

struct Card {
    let color: String
    let shape: String
    let count: Int
    let shade: String

    init(color: String, shape: String, count: Int, shade: String) {
        self.color = color
        self.shape = shape
        self.count = count
        self.shade = shade
    }
}


func buildDeck() -> [Card] {

    var deckOfCards: [Card] = []

    for x in color {
        for y in shape {
            for z in count {
                for w in shade {
                    asdf.append(Card(color: x, shape: y, count: z, shade: w))
                }
            }
        }
    }

    return deckOfCards
}

var newDeck = buildDeck()

// This code works
for all in 0 ..< 81 {
    print(newDeck[all])
}

// This code doesn't
for all in newDeck {
    print(newDeck[all])
}
//Playground execution failed: error: testing.xcplaygroundpage:35:18: error: cannot subscript a value of type '[Card]' with an index of type 'Card'

1 Answer

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points

In the code below "all" is sequencing thru the numbers 0-81. Each time assuming that integers value for each iteration thru the for loop. First loop: all = 0, second loop: all = 1, third loop: all = 2, etc.

// This code works
for all in 0 ..< 81 {
    print(newDeck[all])
}

In the code below "all" represents a Card object in the newDeck array. Each time you iterate thru the for loop all assumes a new Card object in the array. First loop: all = Card object at index 0, second loop all = Card object at index 1, third loop all = Card object at index 2, etc. You cant pass a Card as an index for your newDeck array.

for all in newDeck {
    print(newDeck[all])
}

What you really want to do is this...

for card in newDeck {
    print(card)
}

If this helped please mark as correct answer, thanks.

Joshua McMahon
Joshua McMahon
6,778 Points

Jeez, thank you! I used to do some programming in Javascript using the old for loops (for i=0; i<array.size; i++), and it seems I keep confusing the two when writing the body of the loop. UGH!

Thanks again! :D