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
Mark Pittam
1,084 PointsWhat type of data collection have I created?
Hi there, I'm trying to get to grips with Swift as my first programming language, but I'm struggling a little to get my head around the terminology.
I've managed to create some kind of data collection with the following code:
let person1 = (name: "Mark", stats: ["Height": 190, "Weight": 180, "Age": 24])
let person2 = (name: "Dave", stats: ["Height": 186, "Weight": 175, "Age": 29])
but I'm not sure what this is. Is this a Tuple consisting of a String and a Dictionary? Or is there a better description of it?
I can then pass this construct into a function such as:
func compareAge(firstPerson : (name: String, stats: Dictionary<String, Int>), secondPerson: (name: String, stats: Dictionary<String, Int>))-> (name: String, stats: Dictionary<String, Int>){
if firstPerson.stats["Age"] > secondPerson.stats["Age"]{
println("\(firstPerson.name) is older!")
return (firstPerson)}
else {
println("\(secondPerson) is older!")
return (secondPerson)}
}
It all seems to work, but it feels like there's a more elegant way of passing the required arguments into the function and specifying a return but without being sure of the terms, it's hard to find where to start reading up on it!
Hope this makes sense!
Cheers
Mark
1 Answer
Stone Preston
42,016 Pointsyep, thats a tuple whose first member named "name" is a string, and whose second member named stats is a dictionary.
you might be better off creating a person class or struct, and using name and stats as properties of that class or struct.
Mark Pittam
1,084 PointsMark Pittam
1,084 PointsAh, I see! Perfect! Thanks Stone.