Dauris Little

  • About
  • Dauris’s Portfolio
  • Blogging Lyf
  • Contact

Using Enum w/Swift

Avatar photo
Dauris
Friday, 23 October 2020 / Published in iOS, Programming Languages, Swift

Using Enum w/Swift

understand enum is nothing fancy and all languages have their version of it. Let’s first understand what is enumeration which is a data type consisting of a set of named values also referred to as members. Apple definition states:

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

Swift makes enumerations (enums) very flexible and does not have to provide a value for each case. If a “raw” value is provided for each enumeration case, the value can be a string, a character, integer or  floating-point type.

Enum Syntax

implementing enumerations is done with enum keyword and place the definition within your implementation.

enum EnumImplementation {

    // place enum definition
}
//Enum Type Example
enum PokemonGender :String {
    case male
    case female
}
//Enum No Type Example
enum PokemonGender {
    case male
    case female
}

We have created some defined values within the enumeration which are Male and Female and they are now enumeration cases. Use the case keyword to introduce new enumeration cases. 

Note: alternative implementation does exist.

Each enumeration definition defines a brand new type. Obviously naming conventions should be respects so names starting with capital letter.

Moving on we can now set a variable to the enum case: var gender: PokemonGender = PokemonGender.male or we can shorten it to var gender = PokemonGender.male

Enums Partying w/Switch Statements

Note: When using enums with switch statements there is no need to use a default case because all the possible enums members are taken care of. This way our enums just needs to be checked an and if all the membeers of the enum are exhuasted then create a default case.

switch gender {
   case .male: print("Gender is male")
   case .female: print("Gender is female")
   //a default case is not needed
}

Defined Values:

Enum case cannot have a raw value if the enum doesn’t have a raw type

Note: enum defined type cannot have other datatypes within the cases

enum PokemonGender {

    //wrong(error): enum case cannot have a raw value if the enum does not have a raw type
    case male = 1

    //correct
    case female

}

You could use enum with defined values if enum is having a “raw” type:

enum PokemonType: Int{

    case Fire = 1, Water, Grass
}

This now enumerates the values and assigns the pokemon their type. Note that with this declaration we have also added the var type after the name enum. To get the value stored in that in the enum member, you need to access its rawValue property as println("Result -> \(type.Water.rawValue)") //prints "2"

Now let’s consider the above enum of Pokemon Type. If fire equals 1, water is given a value of 3, and if grass is not given any value, then the raw value of grass will give you 4

Enum PokemonType: Int {

    case Fire = 1, Water = 3, Grass
}

print(PokemonType.Grass) //prints "4"

Methods and Enum

You may be thinking that enums and methods are foreign friends but that is not the case. Enums can have methods that use enum cases:

enum DOTW:String {

   case Sunday
   case Monday
   case Tuesday
   case Wednesday
   case Thursday
   case Friday
   case Saturday

   func day() ->String {
      return self.rawValue
   }

}

print(DOTW.Monday.day()) //prints Monday

Important: Enums can have methods, subscripts, and computed properties. But it cannot have stored properties

Tagged under: enum, Enumeration, iOS, Mobile Development, swift

What you can read next

Using let or var within Swift
Gesture Controls for Android w/Kotlin
Android reCAPTCHA
Integrating Google’s reCAPTCHA w/Android

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories

Recent Posts

  • Gesture Controls for Android w/Kotlin

    Incorporating Gesture controls in your android ...
  • Android Rating: In-App Review API

    An app rating and reviews are crucial if you wa...
  • QR Reader in Android w/ Kotlin

    Turn your phone's camera into a QR scanner...
  • Creating Advance Custom Snackbar w/ Kotlin

    Ask 100 different developers about what they fi...
  • Swift Has Tuple

    Swift provides us with a type called Tuple whic...

© 2017. All rights reserved. Designed by Dauris Little

TOP
This site uses tracking cookies to personalize content and ads. AcceptLearn More