Posts

Showing posts with the label Objective C

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

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