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
Roland Hagan
6,443 PointsHow do I get the name of a UIButton after it has been pressed and compare it with an array of items?
The UIButton has an image view embedded within it. I want to apply image names to buttons and then take those names once a button has been pressed. When the UIButton and/or UIImage has been pressed I want to retrieve the name associated with that button/image and compare it with an array. Here is where I am at the moment:
enum Icon: String {
case African = "african"
case Buffet = "buffet"
case Burger = "burger"
case Chinese = "chinese"
case Fries = "fries"
case Grill = "grill"
case IceCream = "icecream"
case Japanese = "jap"
case Mexican = "mex"
case Pizza = "pizza"
case Seafood = "seafood"
case StreetFood = "streetfood"
case Taj = "taj"
case Turkey = "turkey"
case Vegetarian = "vegetarian"
}
struct Food {
let icon: UIImage?
init(foodCategories: [String: AnyObject]) {
if let iconString = foodCategories["icon"] as? String {
icon = foodImageFromIconString(iconString)
}
}
func foodImageFromIconString(iconString: String) -> UIImage {
var imageName: String
if let iconValue = Icon(rawValue: iconString) {
switch iconValue {
case .African: imageName = "african"
case .Buffet: imageName = "buffet"
case .Burger: imageName = "burger"
case .Chinese: imageName = "chinese"
case .Fries: imageName = "fries"
case .Grill: imageName = "grill"
case .IceCream: imageName = "icecream"
case .Japanese: imageName = "jap"
case .Mexican: imageName = "mex"
case .Pizza: imageName = "pizza"
case .Seafood: imageName = "seafood"
case .StreetFood: imageName = "streetfood"
case .Taj: imageName = "taj"
case .Turkey: imageName = "turkey"
case .Vegetarian: imageName = "vegetarian"
}
}
return UIImage(named: imageName)!
}