Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

341
Views
iOS Swift, Enum CaseExtensión iterable

Estoy tratando de escribir una extensión para una enumeración que es CaseIterable para que pueda obtener una matriz de valores sin procesar en lugar de los casos, aunque no estoy completamente seguro de cómo hacerlo

 extension CaseIterable { static var allValues: [String] { get { return allCases.map({ option -> String in return option.rawValue }) } } }

necesito agregar una cláusula where de alguna manera, si no tengo una cláusula where obtengo un error que dice 'map' produces '[T]', not the expected contextual result type '[String]'

¿Alguien sabe si hay una buena manera de hacer esto?

Mis enumeraciones en las que quiero que funcione esta función se ven un poco así

 enum TypeOptions: String, CaseIterable { case All = "all" case Article = "article" case Show = "show" }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No todos los tipos de enumeración tienen un RawValue asociado y, si lo tienen, no es necesariamente un String .

Por lo tanto, debe restringir la extensión a los tipos de enumeración que son RawRepresentable y definir el valor de retorno como una matriz de RawValue :

 extension CaseIterable where Self: RawRepresentable { static var allValues: [RawValue] { return allCases.map { $0.rawValue } } }

Ejemplos:

 enum TypeOptions: String, CaseIterable { case all case article case show case unknown = "?" } print(TypeOptions.allValues) // ["all", "article", "show", "?" ] enum IntOptions: Int, CaseIterable { case a = 1 case b = 4 } print(IntOptions.allValues) // [1, 4] enum Foo: CaseIterable { case a case b } // This does not compile: print(Foo.allValues) // error: Type 'Foo' does not conform to protocol 'RawRepresentable'
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!