Posts

Showing posts with the label swiftBasics

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

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

Swift Optional

Some people found it very confusing initially but when you will understand then it is simple. As we know optional tells itself about it. It's two cases. The first case when a value can be present for a type, then we need to unwrap if need to use it. Second case when a value can not be present for a type. We use '?' to mark a value as optional. class Student {     var name: String ? //  name becomes optional } When you declare the name string as optional by marking ' ? ' then it's valued automatically is set to nil.  NOTE: Swift 'nil' is not same as objective c 'nil'. In Objective-C, it is a pointer to an object which does not exist whereas in swift it's just an absence of a value for a type. let person = Student() print( person .name) When you access the name it will print a  nil . As there is no value assigned to name. Once we will provide a value to name, let say, "Swift". Then it looks like....

Functions in Swfit 3.0

As you have seen the previous post on Protocol , NSBlockOperation , Range Operators , Extension . Here you go with the new topic as Function in Swift 3.0.1 with Xcode 8.1. It will include detailed explanation about it. According to Apple’s  Swift Programming Language Book : Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. Classes, structures, and enumerations can also define type methods, which are associated with the type itself. Type methods are similar to class methods in Objective-C. Functions A function contains some lines code which performs a specific task. You need to a name function, which explains what it does, call it whenever you need it. Defining and Calling Functions: Defining function -  func wishBday ( person : String ) -> String { let wishe...