How would you describe a Swift language?

 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 functions, and classes can inherit attributes from each other.We'll dive into classes, objects, properties, functions, and a concept called inheritance.

  • Reusability of code through inheritance.
  • Polymorphism flexibility.
  • Encapsulation

Drawbacks

  • Swift is a single inheritance language, and we can have only one superclass for the animal classes, that superclass will need to contain the code required for each of the three categories.
  • Inheritance of functionalities that a type does not need. It can lead to bloated superclasses because we may need to include functionality that is needed by only a few of the subclasses.
  • We can’t create constants in our superclass that can be set by the subclasses.
  • In Swift, Value types cannot use inheritance, only Reference types.


    Protocol-oriented: 

    • Protocol-oriented design allows us to work with classes, structures and enumerations.
    • We can use protocol extensions to add functionality to types that conform to our protocols.
    • Ability to define any of the properties as constants.
    • Protocol composition allows a data structure to implement multiple requirements.
    • Cleaner code.
    •  Instead Swift Protocols have a rule changer feature: Protocol Extensions.
    • Drawback: Abuse of protocol inheritance and protocol extensions could lead to a complex system.
    the protocol-oriented design focuses on implementing a protocol that’s concerned about the requirements other than the details as designed in the objected-oriented design. 


    Functional Programming:

    • In functional programming, data cannot be stored in objects and it can only be transformed by creating functions. In object-oriented programming, data is stored in objects.
    • In other words, functional programming promotes code with no side effects, no change of value in variables.

    Declarative programming:

    Declarative programming is a non-imperative style of programming in which programs describe their desired results without explicitly listing commands or steps that must be performed. 
    e.g.
    func filter(array: [Person], name: String) -> [Person] {
    return array.filter({ $0.name == name })
    }

    Imperative Programming:

    func filter(array: [Person], name: String) -> [Person] {
    var result = [Person]()
    for item in array {
    if item.name == name {
    result.append(item)
    }
    }
    return result
    }

    Same defiance between SwiftUI(declarative) and UIKit(imperative)



    Comments

    Popular posts from this blog

    iOS Questions (Swift Programming Language)

    Dispatch techniques: Dynamic dispatch and Static Dispatch

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