Posts

Enum raw value

 They are the values by which the enum is pre-populated.  Eg enum ControlCharacter:Character {     case tab = "\t"      case return = "\r" } In this enum tab has the default value of "\t" If you dont specify any raw value swift will automatically assign values  Eg enum Direction:String { case north , south , east , west    } Here Direction.east has a default raw value east. 

Enums with associated values in swift

Enums with associated values Enums are a collection of distinct values that some how belong together. An example of an enum would be enum Gender { case male   case female   case others } Here use can see that the enum gender is a collection of genders. For most cases these kind of simple enums would suffice. But for complex cases this wont suffice. Lets look into a example, lets make a enum for celestial bodies enum CelestialBodies {  case star case blackHoles case comets case astroids case planet } The enum celestial bodies does not hold any additional info about the cases. Lets alter it a bit.   1. Add size attribute to star   2. Add info comet to check if pose any threat to earth and its closest distance to earth   3. Add info to planet whether it can support life enum  CelestialBodies {   case star(size:Int)   case blackHoles   ...

Enums in swift

Traditionaly enumerations are used to represent a type that can take definite values. Eg enum Gender { case male case female case others } let genderOfPerson = Gender.male Using gender in switch statement switch genderOfPerson { case .male:{ print("Male is the gender") } case .female:{ print("Female is the gender") } case .others:{ print("others is the gender") } }