Introduction to Swift

Swift is a general-purpose, multi-paradigm, object-oriented, functional, imperative and block structured language. It is the result of the latest research on programming languages and is built using a modern approach to safety, software design patterns by Apple Inc.  It is the brand new programming language for iOS application, macOS application, watchOS application, tvOS application. Soon it became one of top 5 programming language and gained popularity among Apple developer community over the few years of time replacing the old school Objective C. Evolution of Swift Programming 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. Soon, It underwent an upgrade to version 1.2 during 2014Swift 2.0 was introduced at WWDC 2015. Initially, version 2.2 was made open-source software under the Apache License 2.0 on December 3, 2015,  for Apple and Linux platforms. It is primarily designed to work with Apple’s Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products. Swift language has gone through major changes since its release from version names 1.0, 2.0, 3.0 and 4.0 and later. The current released version is Swift 4.2 with a beta version of Swift 4.3 and Xcode 10. Changes include the following areas:

  • Syntax change
  • Library and methods names change
  • New features integration
  • The newly added library like Core ML and AR kit and Vision frameworks Major and promising changes in the latest version i.e. Swift 4 and later includes the following:
  • Faster, easier to use Strings that retain Unicode correctness and add support for creating, using and managing substrings.
  • Smart key paths for type-safe, efficient, extensible key-value coding for Swift types.
  • Enhancements to creating and manipulating Dictionary and Set types.
  • Extends support of archival and serialization to struct and enum types and enables type-safety for serializing to external formats such as JSON and plist.
  • Enforced exclusive access to memory.
Programming in Swift
// Basic Swift Program
import UIKit
var str1 = "Hello geeks!"
var str2 = "How are you?"
print (str1)
print (str2)

Output:

Hello geeks!
How are you?

Run:  Code can be tested on Online IDE for Swift Note: Import statement is used to import any objective-C framework or library directly into Swift program. var keyword is used for variable and let keyword is used for constant. There is no need of ; for termination, in case programmer uses it compiler won’t show error.

General Features of Swift programming language
  • The Protocol-Oriented Programming Paradigm in Swift: Protocol-Oriented Programming is a new programming paradigm used from the release time of Swift 2.0. In this approach, design protocols are similar to classes but this serves better as compared to object-oriented programming. Since the concepts like structs and enums don’t work properly as a struct cannot inherit from another struct, neither can an enum inherit from another enum. So inheritance which is one of the fundamental object-oriented concepts cannot be applied to value types. On the other hand, value types can inherit from protocols. The concepts used in protocol-oriented paradigm are:
  1. Protocol extensions
  2. Protocol inheritance
  3. Protocol compositions
  • Optional type variable in Swift: There is a rule in Swift that each declared variable must have a value associated with them while running the application. In case the variable’s value is found null or nil, the app crashes. So Apple’s engineer came up with a solution in a very smooth and intelligent manner with a concept what they called as optional. While declaring any variable. For Example:
var number: Int?

? ‘ is a kind of notation and the variable associated with optional it is called an optional type variable. A variable declared with an option is basically a safe variable that value if found nil then, Xcode and app just ignore that variable and doesn’t crash. The concept of safe unwarping for optional is used to achieve this functionality.

  • Encodables, Decodables and Delegate methods: In most apps or rather say each and every day to day applications use data and, the data security is a major concern. The apps involve network connection, saving data to disk, or submitting data to APIs and services. These tasks data needed to be encoded and decoded to and from an intermediate format while the data is being transferred. Apple has made their own libraries to cope up with these issues i.e Encodable and Decodable. These are Swift standard library defined for a standardized approach to data encoding and decoding. Delegates methods are the part of Protocol-oriented approach and abstract class implementation in Swift. Advantages:
  • Swift is open sourced and easy to learn.
  • Swift is fast, safe and expressive.
  • Swift is approachable and familiar C and C++ or Rust code can be added by Swift programmers into Swift applications.
  • Swift is the future of Apple development.
  • Swift is enterprise ready. Disadvantages:
  • The language is still quite young and talent pool is limited.
  • Swift is considered a “moving target” as it is a new language and number of swift programmers are few.
  • Poor interoperability with third-party tools and IDEs
  • Lack of support for earlier iOS versions.