Posts

Showing posts from July, 2017

Optional Chaining

Apple says "Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil" . While processing if optional has value then the process will succeed otherwise the process will return nil . This querying, calling properties, methods and subscribe can be chained together and it failed gracefully if chain returns nil . An Alternative to Forced unwrapping: you will add question mark '?' after the optional value on which property you will wish to call properties, method, and subscripts if the optional is non- nil . Could an important interview question - The main difference between optional chaining and forced unwrapping is optional chaining fails gracefully whereas forced unwrapping will give runtime error when the optional value is nil . The result of optional chaining will return the same type value but that value will be wrapped with optional. So if Int is return t

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