Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Associated value or extension for an Enum

Tags:

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?

like image 227
Avner Barr Avatar asked Apr 28 '15 09:04

Avner Barr


People also ask

What is associated value in enum Swift?

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.

Can enum be extended in Swift?

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.

Is enum value type in Swift?

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.

How does enum work in Swift?

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.


2 Answers

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
like image 102
Kirsteins Avatar answered Sep 18 '22 15:09

Kirsteins


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.

like image 24
sketchyTech Avatar answered Sep 21 '22 15:09

sketchyTech