Answers for "can i change enum cases values in swift"

1

swift change enum value

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)
}
Posted by: Guest on March-20-2020

Code answers related to "Swift"

Browse Popular Code Answers by Language