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

Stuck sorting players into experienced vs not

I've created each player in a dictionary

let player1 = ["name": "Joe Smith", "height": "42", "experience": "Yes", "Guardians": "Jim and Jan Smith"]

Then I assigned that to an array

var players = [player1]

I'm struggling with how to access the height key so that I can put the players into experienced vs not.

2 Answers

To get the height (although if you're wanting to categorize by experience, shouldn't you be wanting to check the player's "experience" key instead?):

let height = players[0]["height"]
let experience = players[0]["experience"]

I figured it out

I did this. I'm type casting "hasExperience" as a boolean and checking that to sort them into 2 arrays of dictionaries.

for player in players {
    if player["hasExperience"] as? Bool == true {
        playersWithExperience.append(player)
    } else if player["hasExperience"] as? Bool == false {
        playersWithoutExperience.append(player)
    } else {
        print("You need to ask the player if they have experience.")
    }

}

What I'm struggling with now is how to sort playersWithoutExperience into teams evenly....