Posts

Showing posts with the label Implicitly Unwrapped Optional

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

Implicitly Unwrapped Optional

As you have checked in the last article in which you have learned about the Optional, Optional value and Optional binding. Want to recall here you go -  Take a look! Sometimes you are sure when an optional has a value when it is set very first time. So it’s not necessary to check the if condition and unwrap the optional value for every time when you want to use that value. Because as you will be assuming that there is a value. So these kinds of optional defined as Implicitly unwrapped optional. So we use (!) to put in place of (?) right after the value which you want to make as optional. The primary use of implicitly unwrapped optional during the class initialization. As you might initial an optional value with some value or default value so.  Example: let optionalString: String ? = "optional" let forcedUnwrappedString: String = optionalString ! above line of code clearly, states we are storing the optional string in 'optionalString' and...