General question regarding swift enum.
I want to create an enum of "icon" and "associate" a value to the enum case
enum Icon {
case plane
case arrow
case logo
case flag
}
I want to create an associated image to the enum's value. And also an associated color to the enum value
So for instance if it was possible to do something like:
extension Icon.plane {
var image = {
get {
return UIImage("plane.png")
}
}
var color = {
get {
return UIColor.greenColor()
}
}
}
var image = Icon.arrow.image // the image associated to the enum
var color = Icon.arrow.color // the color associated to the enum
Is this type of thing possible?
In Swift enum, we learned how to define a data type that has a fixed set of related values. However, sometimes we may want to attach additional information to enum values. These additional information attached to enum values are called associated values.
We can extend the enum in two orthogonal directions: we can add new methods (or computed properties), or we can add new cases. Adding new methods won't break existing code. Adding a new case, however, will break any switch statement that doesn't have a default case.
Types in Swift fall into one of two categories: first, “value types”, where each instance keeps a unique copy of its data, usually defined as a struct, enum, or tuple. The second, “reference types”, where instances share a single copy of the data, and the type is usually defined as a class.
Enumerations (or enums for short) in Swift define a common type for a group of related values. According to the Swift documentation, enums enable you to work with those values in a type-safe way within your code. Enums come in particularly handy when you have a lot of different options you want to encode.
Unfortunately you cannot define static properties based on enum cases, but you can use computed properties and switch
to return values for each case:
enum Icon {
case plane
case arrow
case logo
case flag
var image: UIImage {
switch self {
case .plane: return UIImage(named: "plane.png")!
case .arrow: return UIImage(named: "arrow.png")!
case .logo: return UIImage(named: "logo.png")!
case .flag: return UIImage(named: "flag.png")!
}
}
var color: UIColor {
switch self {
case .plane: return UIColor.greenColor()
case .arrow: return UIColor.greenColor()
case .logo: return UIColor.greenColor()
case .flag: return UIColor.greenColor()
}
}
}
// usage
Icon.plane.color
Using enums with associated values combined with switch statements you can be very flexible. A first example:
enum Icon {
case plane(img:UIImage, col:UIColor)
case arrow(img:UIImage, col:UIColor)
case logo(img:UIImage, col:UIColor)
case flag(img:UIImage, col:UIColor)
var values:(img:UIImage,col:UIColor) {
switch self {
case let .plane(image, color):
return (image,color)
case let .arrow(image, color):
return (image,color)
case let .logo(image, color):
return (image,color)
case let .flag(image, color):
return (image,color)
}
}
}
var a = Icon.plane(img: UIImage(named: "image.png")!, col: UIColor.blueColor())
a.values.col
a.values.img
and a second example:
enum Icon {
case plane(img:UIImage, col:UIColor)
case arrow(img:UIImage, col:UIColor)
case logo(img:UIImage, col:UIColor)
case flag(img:UIImage, col:UIColor)
var img:UIImage {
switch self {
case let .plane(image, color):
return image
case let .arrow(image, color):
return image
case let .logo(image, color):
return image
case let .flag(image, color):
return image
}
}
var col:UIColor {
switch self {
case let .plane(image, color):
return color
case let .arrow(image, color):
return color
case let .logo(image, color):
return color
case let .flag(image, color):
return color
}
}
}
var a = Icon.plane(img: UIImage(named: "image.png")!, col: UIColor.blueColor())
a.col
a.img
no need for extensions. And if you really do want static values, you could do this:
struct MyIcon {
static let plane = Icon.plane(img: UIImage(named: "image.png")!, col: UIColor.blueColor())
static let arrow = Icon.arrow(img: UIImage(named: "image.png")!, col: UIColor.blueColor())
static let logo = Icon.logo(img: UIImage(named: "image.png")!, col: UIColor.blueColor())
static let flag = Icon.flag(img: UIImage(named: "image.png")!, col: UIColor.blueColor())
}
MyIcon.arrow.col
which might be tidier than placing the fixed literal values inside a switch statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With