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
Bjorn Morrhaye
2,387 Pointstransform from textField to Enum Value
Hi,
i wrote this as an example. I want to get the bmonth value of type Month from a textfield, How do i do this ?
enum Month {
case January, February, March, April, May, June, July, August, September, October, November, December
}
struct BirthDay {
let day: Int
let month: Month
let year: Int
func calculateWesternZodiac() {
// some long code
}
func calculateVedicZodiac() {
// some long code
}
}
// Hardcoding the values
let myBirthday = BirthDay(day: 02, month: .February, year: 1980)
// values from input textField
let bday = Int(dayTextField.text)
let bmonth = Month(monthTextField.text) // I'm looking for the correct way of doing this !
let byear = Int(yearTextField.text)
let yourBirthday = BirthDay(day: bday, month: bmonth, year: byear)
1 Answer
Bjorn Morrhaye
2,387 PointsOk solved it myself by using raw values (wasn't there in the course yet :p)
enum Month:String {
case January = "January", February, March, April, May, June, July, August, September, October, November, December
}
struct BirthDay {
let day: Int
let month: Month
let year: Int
func getZodiac() -> String {
switch month {
case .February: return "Aquarius"
default: return "Other cases not included in example"
}
}
}
// Hardcoding the values
let myBirthday = BirthDay(day: 02, month: .February, year: 1980)
// values from input textField
let bday = Int("02")! // simulating input textfield.text
let bmonth = Month(rawValue: "February")! // simulating input textfield.text
let byear = Int("02")! // simulating input textfield.text
let yourBirthday = BirthDay(day: bday, month: bmonth, year: byear)