Cookbook – SWIFT for Beginners – Chapter 08 : Enums

Hits: 1

Free eBooks for Beginners

Enums, short for Enumerations, is a data type that is used to define a set of related values in Swift programming language. It is a great tool for organizing and grouping data together.

Think of it like a multiple choice question, where you have a list of options to choose from. This is what enums do, they allow you to define a set of related values in a clean and organized way.

To create an enum in Swift, you use the “enum” keyword followed by the name of the enum and then a set of related values enclosed in curly braces “{}”. For example:

enum Weekdays { 
    case Monday 
    case Tuesday 
    case Wednesday 
    case Thursday 
    case Friday 
    case Saturday 
    case Sunday 

In this example, we created an enum named “Weekdays” that has seven values, one for each day of the week.

Once you have defined an enum, you can use its values in your code. For example:

let today = Weekdays.Monday

Here, we created a constant named “today” and set it to “Weekdays.Monday”, which is one of the values of the “Weekdays” enum.

Enums can also have associated values, which means that each value of the enum can have its own unique data associated with it. For example:

enum StudentGrades { 
    case A(Double) 
    case B(Double) 
    case C(Double) 
    case D(Double) 
    case F(Double) 

In this example, each value of the “StudentGrades” enum has a Double associated with it. This allows you to store additional information with each value.

Enums are a powerful tool in Swift and can help you organize your data in a clear and concise way. They can also be used in switch statements, which allow you to perform different actions based on the value of an enum.

So, if you’re just starting out with Swift programming or if you’re looking to improve your code organization, enums are a great place to start. Start using enums today and see how they can make your code cleaner, more readable, and more efficient!

Cookbook – SWIFT for Beginners – Chapter 08 : Enums

Loader Loading...
EAD Logo Taking too long?

Reload Reload document
| Open Open in new tab

Download PDF [330.40 KB]

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.