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
Harold Davis
Courses Plus Student 14,604 PointsHi Can someone tell me how i can grab a value from a dictionary? I want to put the count of a value from a dict to var
Hi Can someone tell me how i can grab a value from a dictionary? I want to put the count of a value from a dict to var
import UIKit
/////////////////////////////////////////////////
///The dictionary Player Roster (playerHeight)///
/////////////////////////////////////////////////
let Experienced = "Yes"
let Inexperience = "No"
var playerRoster =
["Joe Smith": "yes", "Jill Tanner": Experienced, "Bill Bon": Experienced, "Eva Gordon": Inexperience, "Matt Gill": Inexperience, "Kimmy Stein": Inexperience, "Sammy Adams": Inexperience, "Karl Saygan":Experienced, "Suzane Greenberg": Experienced, "Sal Dall": Inexperience, "Joe Kavalier":Inexperience, "Ben Finkelstein" : Inexperience,"Diego Soto": Experienced, "Chloe Alaska": Inexperience, "Arnold Willis": Inexperience, "Phillip Helm": Experienced, "Les Clay": Experienced, "Herschel Krustofski": Experienced]
/////////////////////////////////////////////////
///The dictionary Player Height (playerHeight)///
/////////////////////////////////////////////////
var playerHeight =
["Joe Smith": 42, "Jill Tanner": 36, "Bill Bon": 43, "Eva Gordon": 45, "Matt Gill": 40, "Kimmy Stein": 41, "Sammy Adams": 45, "Karl Saygan": 42, "Suzane Greenberg": 44, "Sal Dall": 41, "Joe Kavalier": 39, "Ben Finkelstein" : 44,"Diego Soto": 41, "Chloe Alaska": 47, "Arnold Willis": 43, "Phillip Helm": 44, "Les Clay": 42, "Herschel Krustofski": 45]
////////////////////////////////////////////////////////
///Function to Import New Players into the dictionary///
////////////////////////////////////////////////////////
func playerInfo(Name Name: String, Experience: Bool, Height: Int, Guardian: String) {
if Experience == false {
playerRoster.updateValue(Inexperience, forKey: Name)
} else {
playerRoster.updateValue(Experienced, forKey: Name)
}
playerHeight.updateValue(Height, forKey: Name)
}
/////////////
///Example///
/////////////
playerInfo(Name: "Harold Davis", Experience: false, Height: 43, Guardian: "Brenda")
////////////////////
///Dragons Roster///
////////////////////
var Dragons = ["":Experienced]
////////////////////
///Sharks Roster////
////////////////////
var Sharks = ["":Experienced]
////////////////////
///Raptors Roster///
////////////////////
var Raptors = ["":Experienced]
2 Answers
garyguermantokman
21,097 Pointslet dict = ["name": "Gary"]
let myName = dict["name"]
Harold Davis
Courses Plus Student 14,604 PointsThank you both! I appreciate the tip! And I'll give this a shot.
Harold Davis
Courses Plus Student 14,604 PointsThank you both! I appreciate the tip! And I'll give this a shot.
Reed Carson
8,306 PointsReed Carson
8,306 Pointsjust a tip, for naming conventions in Swift we dont capitalize properties or parameter names. We do capitalize custom types like class, struct and protocol names.