Posts

Showing posts from 2017

Enumeration in swift

Enumerations 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. enum Direction { case north case south case east case west } Note : Unlike C and Objective C, Here enum is not initialized with a default value.  The name must start with a Capital letter. var direction = Direction. west or var direction = . west If the  case  for  .west  is omitted, this code does not compile, because it does not consider the complete list of  CompassPoint  cases. When it is not appropriate to provide a  case  for every enumeration case, you can provide a  default  case to cover any cases that are not addressed explicitly: switch somePlanet { case . earth : print ( "Mostly harmless" ) default : print ( "Not a safe place for humans" ) } Associated Values      You can define Swift enumerations to store a

Swift: Type Casting

Image
Type Casting: Using type casting you will get to know about the type of an instance and at the same time, you can treat as a different super class or subclass hierarchy from its own class hierarchy. We use two different operators to do type casting. as is It can be checked for protocol conformance as well. we will see with an example:      we created an array of with objects which can have Movie and Song object. However, if you iterate over the contents of this array, the items you receive back are typed as  MediaItem , and not as  Movie  or  Song . Checked the below image and if you need to use contents of the array then you have type cast or downcast in a different type. Checking Type: Use the  type check operator  ( is ) to check whether an instance is of a certain subclass type.  The type check operator returns  true  if the instance is of that subclass type and  false  if it is not. for item in library {     if item is Movie { // r