我可以更改enum的关联值吗?

时间:2021-11-01 10:00:26

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

我正在看Swift旅游文件,遇到了一个问题。这是代码:

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?

函数adjust()不起作用,我想知道是否有方法可以更改enum的关联值,以及如何更改?

1 个解决方案

#1


23  

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.

你的最直接的问题是,你试图改变一个不变的变量的值(宣布让)当你应该用var声明它。这不会解决这个特定问题虽然自你的名字变量包含关联值的副本,但总的来说这是你需要注意的。

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:

如果您想要解决这个问题,您需要将adapt()函数声明为一个突变函数,并根据具体情况将self重新赋值为一个新的enum值,该值具有由旧值和新值组成的关联值。例如:

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)
}

#1


23  

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.

你的最直接的问题是,你试图改变一个不变的变量的值(宣布让)当你应该用var声明它。这不会解决这个特定问题虽然自你的名字变量包含关联值的副本,但总的来说这是你需要注意的。

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:

如果您想要解决这个问题,您需要将adapt()函数声明为一个突变函数,并根据具体情况将self重新赋值为一个新的enum值,该值具有由旧值和新值组成的关联值。例如:

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)
}