Posts

Showing posts with the label Range Operator

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  fal...

Swift - 3.0 : Range Operators

Range Operators:  In order to avoid traditional way of iterating through the range of values, Swift has introduced, shorthand, handy, Range Operators. Swift has two type of range operators by which you can iterate through the range of values. These are shorthand for to avoid traditional way of iterating through values using for loop etc. Traditional Iteration:  for (var i = 0 ; i<= 5 ;i += 1 ) {     // for iterating through values } Range Operator Type: Closed Range Operator  (a ... b) - range includes a and b. Half - Open Range Operator (a ..< b ) range includes an only. 1. Closed Range Operator - This operator defines as (x ... z) values from x to z. In this range of values 'x' and 'z' both are included in the range . But you should remember that value 'x' must not be greater than 'z' value. The compiler will give an error. If 'x' and 'z' are same then it will run successfully. Example: Just adding v...