Posts

Showing posts from February, 2021

What’s the difference between var and let? Which one would you choose for properties in a struct and Class why?

Image
Let is an immutable variable, meaning that it cannot be changed, other languages call this a constant. Var is a mutable variable, meaning that it can be changed.   “Use let to make a constant and var to make a variable” let aPerson = Person (name: Foo , first: Bar ) //< data of aPerson are changeable, not the reference var aPerson = Person (name: Foo , first: Bar ) //< both reference and data are changeable. var aPersonA = Person (name: A , first: a) var aPersonB = Person (name: B , first: b) aPersonA = aPersonB //aPersonA now refers to Person (name: B , first: b) Value and Reference Type Reference Type(Class) Swift's  classes  are  mutable  a-priory var  +  class It can be  reassigned  or  changed let  +  class  =  constant of address It can  not  be  reassigned  and can be  changed Value(Struct, Enum) Swift's  struct  can change th...

Dispatch techniques: Dynamic dispatch and Static Dispatch

4 types of dispatch techniques — Inline  (Fastest) Static Dispatch Virtual Dispatch Dynamic Dispatch  (Slowest) Dynamic dispatch Swift  allows a class to override methods and properties declared in its superclasses. ... This technique, called  dynamic dispatch , increases language expressivity at the cost of a constant amount of runtime overhead for each indirect usage. Method Dispatch is the mechanism that helps to decide which operation should be executed, or more specifically, which method implementation should be used. Dynamic Dispatch  is supported only by  reference types ( i.e.  Class). The reason for this is that, for  dynamism , or  dynamic dispatch,  in short, we need  inheritance  and our  value types  do not support inheritance. To achieve  Dynamic Dispatch , we use inheritance, subclass a base class and then override an existing method of the base class. Also, we can make use of  dynamic  k...

How would you describe a Swift language?

Image
  Swift: Language was developed by ‘ Chris Lattner ‘ with an aim to resolve difficulties existed in Objective C. It was introduced at  Apple’s 2014 Worldwide Developers Conference (WWDC)  with version  Swift 1.0 .  Swift is a type safe Multi-paradigm: protocol-oriented, object-oriented,  functional , imperative, block structured, declarative programming. Type-safe:  Swift is a  type-safe  language, which means the language helps you to be clear about the types of values your code can work with. If part of your code requires a  String , type safety prevents you from passing it an  Int  by mistake. Likewise, type safety prevents you from accidentally passing an optional  String  to a piece of code that requires a non-optional  String . Object-oriented: Object - Oriented  Programming (  OOP  ) helps you structure your  Swift  code with so-called classes. These classes have properties and fun...