Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
Swift enum values not settable

I am adding some syntactical sugar to my app, and I came across an 'inconsistency'. There is probably good reason for it, but is there way to clean the implementation a little. I a doing this to provide a single place to set/get some properties.

enum MyType: String {
    case Unknown, First, Second

    var enabled: Bool {
        set { UserDefaults.standard.set(newValue, forKey: self.rawValue) }
        get { return UserDefaults.standard.bool(forKey: self.rawValue) }
    }

}

Now I can use the enum values to get the defaults

if MyType.First.enabled {
    ........
}

But I cannot directly set the value:

MyType.First.enabled = true

elicits 'cannot set property, First is not assignable'

but I can get away with

var type = MyType.First
type.enabled = true

Is there a simple way to use the one-line method?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The issue is that enums are value types, so when you call the setter of enabled on MyType.First, you mutate First as well. If you don't assign value types to a mutable variable explicitly, they are considered immutable. This is why if MyType.First.enabled {...} doesn't work, but

var type = MyType.First
type.enabled = true

does work.

If you mark the setter as nonmutating, the issue is resolved, since the compiler will now know that you aren't mutating a value type when calling the setter on its property.

enum MyType: String {
    case unknown, first, second

    var enabled: Bool {
        nonmutating set { UserDefaults.standard.set(newValue, forKey: self.rawValue) }
        get { return UserDefaults.standard.bool(forKey: self.rawValue) }
    }

}

P.S.: the Swift naming convention for enum cases is lowerCamelCase, since they are not types themselves and only types should be UpperCamelCase. I've modified your MyType enum accordingly.

over 4 years ago · Santiago Trujillo Relatório

0

By adding the nonmutating keyword to the set method you are telling the compiler that the set method will not modify the enclosing type, that is the enum MyType in this case

enum MyType: String {
    case Unknown, First, Second

    var enabled: Bool {
       nonmutating set { UserDefaults.standard.set(newValue, forKey: self.rawValue) }
       get { return UserDefaults.standard.bool(forKey: self.rawValue) }
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda