Introducci贸n a Swift y XCode
Swift y el ecosistema Apple
Instalaci贸n y configuraci贸n de XCode
Quiz: Introducci贸n a Swift y XCode
Fundamentos de programaci贸n iOS
Variables, constantes y tipos de datos b谩sicos
Condicionales
Funciones
Tipos de datos opcionales
Arreglos: append, insert, como crearlas
Arreglos: validar, editar y agregar una lista dentro de otra lista
Conjuntos: como agregar o eliminar elementos
Conjuntos: principales operaciones entre conjuntos
Diccionarios
Recorridos parte 1: while
Recorridos parte 2: for
Recorridos parte 3: los retos
Quiz: Fundamentos de programaci贸n iOS
POO en iOS
Programaci贸n orientada a objetos en iOS
Structs
Clases y herencia
Enums
Protocolos
Funciones de arreglos (filter, map, reduce)
Funciones de arreglos parte 2 (filter, map, reduce): playground
Quiz: POO en iOS
Manejo de errores y programaci贸n segura
Manejo de errores y programaci贸n segura
Propagaci贸n de errores
Do, try, catch
Quiz: Manejo de errores y programaci贸n segura
Programaci贸n en el ecosistema de Apple
Siguientes pasos para el desarrollo en iOS
You don't have access to this class
Keep learning! Join and start boosting your career
Enums, or enumerables, are data types that allow you to have a limited set of specific choices in your code. We could think of them as a selection menu that ensures that only certain answers can be chosen. Let's imagine we have a class called "Subject" that includes difficulty levels such as basic, intermediate, or advanced. To avoid entering wrong or misspelled options, it is ideal to use enums. This makes the code cleaner, more organized and easier to maintain.
To create an enum in Swift, it is necessary to define it using the enum
keyword, followed by the use cases or possible values it may have. When we model an enum to define levels of a subject, we can set the limited difficulty options.
public enum SubjectLevel { case basic case intermediate case advance }
This simple block of code defines a new data type called SubjectLevel
that can only have one of three values: basic
, intermediate
, or advance
.
Integrating the enum into a class helps to limit the property options to those defined in the enum. By modifying the class of a subject to use our SubjectLevel
, we eliminate the need for diversity in assigning values and avoid errors arising from the use of inappropriate strings.
class Subject { var level: SubjectLevel init(level: SubjectLevel) { self.level = level } }
We use a switch
to evaluate the level and return different values depending on the specific case. This method allows us to define the business logic for each level in a simple and clear way in terms of its expected results.
func minimumScore() -> Double { switch level { case .basic: return 50.0 case .intermediate: return 70.0 case .advance: return 80.0 } }
This code snippet makes it easy to calculate a minimum score based on the subject level, ensuring that the score calculation logic is consistent across instances.
An effective way to verify that the changes and the use of enums work correctly is to create class instances and perform some console tests. By checking how the system interprets different subjects and assigning students to them, you can ensure that the system responds as expected.
let mathSubject = Subject(level: .intermediate) let spanishSubject = Subject(level: .basic) print(mathSubject.minimumScore()) // Prints 70.0 print(spanishSubject.minimumScore()) // Prints 50.0
Looking at these examples, it is clear that using enums in Swift structured a clean, readable code and facilitates revisions. This is essential for large projects where code organization and maintenance are paramount.
Contributions 1
Questions 0
Want to see more contributions, questions and answers from the community?