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

269
Views
¿Es posible agrupar y representar varios casos como otro caso dentro de una enumeración en Swift?

Esto es lo que quiero decir.

 enum Device { case iPhone_13Pro, iPhone_12Pro case iPadPro_129 case iPadPro_11 } // Hypothetical Scenario enum Device_Group { case iPhones case iPads }

¿Hay alguna forma de hacer lo anterior para representar un determinado grupo de casos como este (puede ser otra enumeración O un caso diferente dentro de la misma enumeración, de modo que pueda hacer lo siguiente?

 // DO THIS switch device { case iPhones: print("These are iPhones") case iPads: print("These are iPads") } // INSTEAD OF THIS switch device { case .iPhone_13Pro, .iPhone_12Pro: print("These are iPhones") case .iPadPro_129, .iPadPro_11: print("These are iPads") }

No sé si esta puede ser una pregunta extraña, pero creo que agrupar varios casos tiene cierto beneficio al usar una declaración de cambio. Agradecería cualquier consejo sobre esto. Gracias por adelantado.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede usar Enum anidado y un caso con parámetro

 enum Devices { case phone(iPhone) case tablet(iPad) enum iPhone { case phone7 case phoneX } enum iPad { case mini case pro } } let randomDevice = Devices.phone(.phone7) switch randomDevice { case .phone: print("Its a phone") default: break } // prints "Its a phone"
over 4 years ago · Santiago Trujillo Report

0

Puede definir dos enumeraciones diferentes para iPhones y iPads y luego usarlas como valores asociados para los tipos de dispositivos.

https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html


 enum Ipad { case iPadAir case iPadPro } enum Iphone { case iPhone12 case iPhone13 } enum Device { case iPad(model: Ipad) case iPhone(model: Iphone) } func findDeviceType(device: Device) { switch device { case .iPad: print("iPad") case .iPhone: print("iPhone") } } findDeviceType(device: Device.iPad(model: .iPadAir)) // iPad
over 4 years ago · Santiago Trujillo Report

0

También puede ver OptionSet y su enfoque flexible para agrupar y serializar sus artículos porque tienen valores únicos. Por ejemplo:

 struct Device: OptionSet { let rawValue: Int static let iPhone_13Pro = Self(rawValue: 1 << 0) static let iPhone_12Pro = Self(rawValue: 1 << 1) static let iPadPro_129 = Self(rawValue: 1 << 2) static let iPadPro_11 = Self(rawValue: 1 << 3) static let iPhone: Self = [.iPhone_13Pro, .iPhone_12Pro] static let iPad: Self = [.iPadPro_129, .iPadPro_11] static let all: Self = [.iPhone, .iPad] } let device = Device.iPhone_13Pro if Device.iPhone.contains(device) { print("It's iPhone") }
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!