You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

1 D铆as
7 Hrs
28 Min
20 Seg

Enums

18/25
Resources

What are enums or enumerables in Swift?

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.

How to create and use an enum in Swift?

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.

How to integrate an enum into a class?

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 } }

How to calculate values based on enums?

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.

How to test and verify the correct use of enums?

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

Sort by:

Want to see more contributions, questions and answers from the community?

minimumScore la hice una computed property: ```js let level : SubjectLevel var minimumScore : Double { switch level{ case .basic: return 50.0 case .intermediate: return 70.0 case .advanced: return 80.0 } } ```