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
Daniel Lambrecht
iOS Development Techdegree Student 5,302 Pointsi do not understand at all...
So i am trying to iterate through my array of players, and then i want to assign them to 2 variables. experienced, and unexperienced players.
But i cant get it to work, i think its because i need to somehow unwrap the values of the player in the array players?
Anyways, i have been trying for 2 days now without getting any further, i looked through the community without much luck. and now i just hope someone can guide me in the right direction...
Thanks!
//: Playground - noun: a place where people can play
import UIKit
// Player facts
let joeSmith: [String: Any] = ["Height": "42", "Experience": true, "Guardians": "Jim and Jan Smith"]
let jillTanner: [String: Any] = ["Height": "36", "Experience": true, "Guardians": "Clara Tanner"]
let billBon: [String: Any] = ["Height": "42", "Experience": true, "Guardians": "Sara and Jenny Bon"]
let evaGordon: [String: Any] = ["Height": "45", "Experience": false, "Guardians": "Wendy and Mike Gordon"]
let mattGill: [String: Any] = ["Height": "40", "Experience": false, "Guardians": "Charles and Sylvia Gill"]
let kimmyStein: [String: Any] = ["Height": "41", "Experience": false, "Guardians": "Bill and Hillary Stein"]
let sammyAdams: [String: Any] = ["Height": "45", "Experience": false, "Guardians": "Jeff Adams"]
let karlSaygan: [String: Any] = ["Height": "42", "Experience": true, "Guardians": "Heather Bledsoe"]
let suzaneGreenberg: [String: Any] = ["Height": "44", "Experience": true, "Guardians": "Henrietta Dumas"]
let salDali: [String: Any] = ["Height": "41", "Experience": false, "Guardians": "Gala Dali"]
let joeKavalier: [String: Any] = ["Height": "39", "Experience": false, "Guardians": "Sam and Elaine Kavalier"]
let benFinkelstein: [String: Any] = ["Height": "44", "Experience": false, "Guardians": "Aaron and Jill Finkelstein"]
let diegoSoto: [String: Any] = ["Height": "41", "Experience": true, "Guardians": "Robin and Sarika Soto"]
let chloeAlaska: [String: Any] = ["Height": "47", "Experience": false, "Guardians": "David and Jamie Alaska"]
let arnoldWillis: [String: Any] = ["Height": "43", "Experience": false, "Guardians": "Claire Willis"]
let phillipHelm: [String: Any] = ["Height": "44", "Experience": true, "Guardians": "Thomas Helm and Eva Jones"]
let lesClay: [String: Any] = ["Height": "42", "Experience": true, "Guardians": "Wynonna Brown"]
let herschelKrustofski: [String: Any] = ["Height": "45", "Experience": true, "Guardians": "Hyman and Rachel Krustofski"]
// Collection of all players
let players = [joeSmith, jillTanner, billBon, evaGordon, mattGill, kimmyStein, sammyAdams, karlSaygan, suzaneGreenberg, salDali, joeKavalier, benFinkelstein, diegoSoto, chloeAlaska, arnoldWillis, phillipHelm, lesClay, herschelKrustofski]
// collections of experienced/unexpericened players
var experiencedPlayers: [[String: Any]]
var unexperiencedPlayers: [[String: Any]]
// Collections of players in each team
var teamSharks: [[String: Any]]
var teamDragons: [[String: Any]]
var teamRaptors: [[String: Any]]
// Team creator
for player in players {
if player["Experience"] == true {
print("\(player)")
} else {
print("oh no")
}
}
1 Answer
Marc Biggar
12,486 PointsHey
Struggled to figure this one out, however you could create the players as objects and then create a func that checks if the player object is experienced and then return that player:
Like so
import UIKit
class Player {
let name: String
let height: Int
let experience: Bool
let guardians: String
let explayers: [Player] = []
init(name: String, height: Int, experience: Bool, guardians: String) {
self.name = name
self.height = height
self.experience = experience
self.guardians = guardians
}
}
let joeSmith = Player(name:"Joe Smith", height: 42, experience: true, guardians: "Jim and Jan Smith")
let billBon = Player(name: "Bill Bon", height: 36, experience: true, guardians: "Clara Tanner")
let sammyAdams = Player(name: "Sammy Adams", height: 45, experience: false, guardians: "Jeff Adams")
let players = [joeSmith, billBon, sammyAdams]
func isExperienced() {
for x in players {
if x.experience == true {
print(x.name)
} else {
print ("Oh no")
}
}
}
isExperienced()
hope thats helps
Daniel Lambrecht
iOS Development Techdegree Student 5,302 PointsDaniel Lambrecht
iOS Development Techdegree Student 5,302 PointsHi thanks for the answer, im afraid im not allowed to use classes yet, but i actually found another way to do it. But here comes the next question:
how do i append the player to the array, instead of the dictionary values of the player? :D
do you know what to do?