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

Wylan Neely
Courses Plus Student 1,518 PointsI need help on how to properly set up this program, do i use a struct, class, enum. I gave it a go, but know it's shotty
I want to make a simple program where you enter a persons age, gender, and amount of daily activity, to come up with a Daily calorie amount specific to each person. but i dont really know how to properly set something like that up. Here is my code so far
the data is from this website chart (https://www.getfit.tn.gov/fitnesstracker/calorie_levels.pdf)
'''swift
enum GenderType {
case ActiveMale
case ActiveFemale
case ModerateMale
case ModerateFemale
case SedentaryMale
case SedentaryFemale
}
/created a function where you enter a gender and howActive someone is, then it assigns a GenderType. I know there is a cleaner way to enter the age in this same function, i just dont know how to lay it out in a clean looking way. So i created a seperate function below called caloriesDaily , that takes in the GenderType, then paired it with an age group to assign the calorie amount/
func isActiveMaleOrFemale(gender: String, howActive: String) -> (GenderType) {
if (gender == "Male" && howActive == "Active") {
return GenderType.ActiveMale
}
else if (gender == "Male" && howActive == "Moderate") {
return GenderType.ModerateMale
}
else if (gender == "Male" && howActive == "Sedentary") {
return GenderType.SedentaryMale
}
else if (gender == "Female" && howActive == "Active") {
return GenderType.ActiveFemale
}
else if (gender == "Female" && howActive == "Moderate") {
return GenderType.ModerateFemale
}
else if (gender == "Female" && howActive == "Sedentary") {
return GenderType.SedentaryFemale
}
else {
return GenderType.ModerateMale
}}
func caloriesDaily(gen: GenderType, age: Int) -> Int {
if gen == GenderType.ActiveMale {
switch age {
case 0...2: return 1000
case 3: return 1400
case 4,5: return 1600
case 6,7: return 1800
case 8,9: return 2000
case 10,11: return 2200
case 12: return 2400
case 13: return 2600
case 14: return 2800
case 15: return 3000
case 16...18: return 3200
case 19...35: return 3000
case 36...55: return 2800
case 56...75: return 2600
case 76...140: return 2400
default: return 0
if gen == GenderType.ModerateMale {
switch age {
case 0...2: return 1000
case 3: return 1400
case 4,5: return 1400
case 6,7,8: return 1600
case 9,10: return 1800
case 11: return 2000
case 12,13: return 2200
case 14: return 2400
case 15: return 2600
case 16...25: return 2800
case 26...45: return 2600
case 46...65: return 2400
case 66...750: return 2200
default: return 0
}
if gen == GenderType.SedentaryMale {
switch age {
case 0...3: return 1000
case 4,5: return 1200
case 6,7,8: return 1400
case 9,10: return 1600
case 11,12: return 1800
case 13,14: return 2000
case 15: return 2200
case 16...18: return 2400
case 19,20: return 2600
case 21...40: return 2400
case 41...60: return 2200
case 61...150: return 2000
default: return 0
}
if gen == GenderType.ActiveFemale {
switch age {
case 0...2: return 1000
case 3: return 1400
case 4: return 1400
case 5,6: return 1600
case 7,8,9: return 1800
case 10,11: return 2000
case 12,13: return 2200
case 14...30: return 2400
case 31...60: return 2200
case 61...750: return 2000
default: return 0}
if gen == GenderType.ModerateFemale {
switch age {
case 0...2: return 1000
case 3: return 1300
case 4,5,6: return 1400
case 7,8,9: return 1600
case 10,11: return 1800
case 12...18: return 2000
case 19...25: return 2200
case 26...50: return 2000
case 51...150: return 1800
default: return 0}
if gen == GenderType.SedentaryFemale {
switch age {
case 0...3: return 1000
case 4,5: return 1200
case 6,7,8: return 1200
case 9,10: return 1600
case 11,12: return 1800
case 13,14: return 2000
case 15: return 2200
case 16...18: return 2400
case 19,20: return 2600
case 21...40: return 2400
case 41...60: return 2200
case 61...150: return 2000
default: return 0}
}
}else {return 0}
'''
/* how would you create a class or structure to make it where i enter 3 properties into the same code block, and it returns a calorie amount.*/
Something like this:
Person (gender: String, age: Int, activityAmount: String) -> (caloriesReccomended: Int){}
1 Answer

Jake Adams
1,608 PointsI think the first thing to focus on (and should help to flush out the rest of the design) is the enum that you have. All the if
statements and string comparisons are a code smell when you consider that Swift wants you to write type-safe code.
Enums are quite powerful and flexible. They can have raw values (underlying values) or associated values (see below). They are also a lot like classes in that they support instance methods. A good course to go through is Swift 2.0 Enumerations & Optionals
You could consider using Associated Values like this:
enum Lifestyle {
case Active
case Moderate
case Sedentary
// instance funcs here
}
enum Person {
case Male(lifestyle: Lifestyle, age: Int)
case Female(lifestyle: Lifestyle, age: Int)
// instance funcs here
}
let him = Person.Male(lifestyle: .Active, age: 20)
let her = Person.Female(lifestyle: .Moderate, age: 29)
There will still be some additional complexity as you sort out the best way to calculate calories, but I hope this will help you start with a more Swifty design.
These enums could be used in conjunction with a class or a struct as well. The difference between a class and struct is basically in how they are passed around. A class is passed by reference and a struct is passed by value. More can be read at Stackoverflow.
Wylan Neely
Courses Plus Student 1,518 PointsWylan Neely
Courses Plus Student 1,518 PointsThank You!, i will look into those courses.