Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Utilize Enumerations

This video was last updated on Jun 28 2022

Learn how to group your constants together in one construct known as an enumeration and how to use it in your code.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Before moving forward into the next and final part of this course, we’re going to explore another type that will help clarify your code. They are called enumerations. Often times in programming, you will hear the expression, ‘avoid magic numbers’. Take a look at this code. Here we are checking that the current item is greater than eighteen.

var damageModifiers = [1, 2, 4];
var currentDamageModifier = 0;
var baseDamage = 100;
enum DamageType { regular, twice, quad }
 var currentDamageType = DamageType.twice;
switch (currentDamageType) {
    case DamageType.regular:
        currentDamageModifier = damageModifiers[0];
        break;
    case DamageType.twice:
        currentDamageModifier = damageModifiers[1];
        break;
    case DamageType.quad: 
        currentDamageModifier = damageModifiers[2];
        break;
}
print('You do ${baseDamage * currentDamageModifier } damage');
/**
  switch (currentDamageType) {
    case DamageType.regular:
      damageModifier = damageValues[0];
      break;
    case DamageType.twice:
      damageModifier = damageValues[1];
      break;
    case DamageType.quad: 
      damageModifier = damageValues[2];
      break;
  }
**/
currentDamageModifier = damageModifiers[currentDamageType.index];

Reviews

Comments