28

I am reading the Swift tour document, and facing a problem. Here is the code:

enum SimpleEnum {
    case big(String)
    case small(String)
    case same(String)

    func adjust() {
        switch self {
        case let .big(name):
            name +=  "not"
        case let .small(name):
            name +=  "not"
        case let .same(name):
            name +=  "not"
        }
    }
}

The function adjust() won't work, I wonder if there is a way to change the Associated value of an enum, and how?

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
wj2061
  • 6,778
  • 3
  • 36
  • 62
  • 2
    It's worth noting that an enum that has the same associated values for each case is *much* better modelled as a struct – e.g `struct S { enum Kind { case big, small, same }; var kind: Kind; var name: String }`. This greatly simplifies such mutations. – Hamish Sep 27 '18 at 15:34

1 Answers1

61

Your most immediate problem is that you're attempting to change the value of an immutable variable (declared with let) when you should be declaring it with var. This won't solve this particular problem though since your name variable contains a copy of the associated value, but in general this is something that you need to be aware of.

If you want to solve this, you need to declare the adjust() function as a mutating function and reassign self on a case by case basis to be a new enum value with an associated value composed from the old one and the new one. For example:

enum SimpleEnum{
    case big(String)
    case small(String)
    case same(String)

    mutating func adjust() {
        switch self{
        case let .big(name):
            self = .big(name + "not")
        case let .small(name):
            self = .small(name + "not")
        case let .same(name):
            self = .same(name + "not")
        }
    }
}

var test = SimpleEnum.big("initial")
test.adjust()

switch test {
case let .big(name):
    print(name) // prints "initialnot"
case let .small(name):
    print(name)
case let .same(name):
    print(name)
}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281