Swift is a general purpose, powerful, compiled programming language for ios, macOS, watchOS, tvOS, Linux and z/OS.It was developed by Apple Inc.

It is  a safe, fast, and interactive programming language with  features of modern language.Swift language is easy to learn for new programmers.Its syntax is expressive and it’s a fun to write  script style code of Swift.

Swift is designed as an alternative to Objective-C

1) The syntax of Swift is much easier than Objective-C.The concepts of the language are based on Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and others.

2) Maintenance of Swift code is easy. In Objective-C  there are two files , header file with extension .h and implementation file .m.Changes in one file can affect other file.So developer has to take care that both the files change accordingly. But in Swift you have to manage only one file with extension .swift.

3) Swift code is precise.Code of Objective-C is lengthy and different than other languages.Whereas Swift code is concise and expressive.

4) Swift is safe to use.Swift eliminates entire classes of unsafe code. Variables are always initialized before use, arrays and integers are checked for overflow, memory is automatically managed, and enforcement of exclusive access to memory guards against many programming mistakes.

Another safety feature is that by default Swift objects can never be nil. In fact, the Swift compiler will stop you from trying to make or use a nil object with a compile-time error.

However, there are cases where nil is valid and appropriate. For these situations Swift has an innovative feature known as optionals. An optional may contain nil, but Swift syntax forces you to safely deal with it using the ? syntax to indicate to the compiler you understand the behavior and will handle it safely.

5) Swift provides Automatic memory management.
Swift uses Automatic Memory Counting (ARC) a technology  added a garbage collector function that wasn’t introduced to iOS before. Languages like Java, C#, and Go use garbage collectors to delete class instances that are no longer used. Before ARC, iOS developers had to manage memory manually and constantly manage retain counts of every class.

6) Swift Supports Dynamic Libraries
Dynamic libraries are executable chunks of code that can be linked to an app. This feature allows current Swift apps to link against newer versions of the Swift language as it evolves over time. Dynamic libraries in Swift are directly uploaded to the memory, cutting down on the initial size of the app and ultimately increasing app performance.

7) Swift is free and open source, and it’s available to developers, educators and students under the Apache     2.0 open source licence.

8) Supports Development  in future
Swift will not only supersede Objective-C for iOS app development but it will also replace C for embedded programming on Apple platforms.

Great apps like Khan Academy, Clear, Sky Guide, LinkedIn, Airbnb , Eventbrite and Hipmunk are example of apps built using Swift.

Swift combines some features of other languages.Named parameters ,feature of  Objective C  is  written with a clean syntax which makes the APIs much easier to maintain and read. Some other important features of swift are –

* Multiple return values and Tuples.
* Generics are powerful and simple to use.
* Concise and fast iteration over a collection or range.
* Structs which support extensions, methods and protocols.
* Enums can have payloads and support pattern matching
* Advanced control flow
* error handling (try / catch / throw )

umang_india

Following IDEs can be used with Swift-

  1. Xcode – In most cases, Xcode will be enough for Swift engineering .
  2. Atom – Atom itself is basic, it has a great number of open source packages built by the GitHub community which will allow you to customize the IDE for cross-platform and versatile development with autocompletion, advanced navigation, and other useful features.
  3. AppCode – This IDE for iOS and macOS developers was designed by JetBrains.

Swift with example –

Folowing is the code snippet for  Simple app to display  “Hello world!” message –

print(“Hello, world!”)

// Prints “Hello, world!”
This one line code is enough for your first application.You don’t need to import separate libraries for string handling.As the code is written at global scope is taken as entry point so do no need to write main function.

Variable declaration –

Vaiable are created with keyword var and constants with keyword let.

var anum = 10
bnum = 20
let constNum = 21

A constant or variable must have the same type as the value you are going to assign it.

Datatype Conversion –

Values are nver implicitly converted into other datatypes.You have to explicitly convert it into other datatype.

  1. let  lblcaption = “The price = ”
    let price = 100
    let widthLabel = lblcaption + String(price)

Other way to convert  the value into string is- Write the value in parentheses, and write a backslash (\) before the parentheses. For example:

let days = 5

let work = “I worked  \(days) .”

Control structure –

Swift allowscontriol structues like  if and switch to make conditionals, and  for-in, while, and repeat-while to make loops. Parentheses around the condition or loop variable are optional. Braces around the body are required.
For example –

if expression {

// statements

}

Migrating Your Objective-C Code to Swift-

Objective-C was the primary programming language used for creating OSX and iOS applications. Objective-C is fundamentally a superset of C with added object-oriented features and dynamic runtime.

In 2014 Apple introduced a  programming language i.e Swift which was described as ‘bjective-C without the C’.

As Swift is fast, safe, modern, and easy to use developers choose Swift over Objective-C.

Performance of your Objective-C applicationcan be improved by replacing code of it in Swift.  You don’t need to rewrite your entire app in Swift at once.

Code Migration

The code  migrating  to Swift should be on a per-file basis  that is, one class at a time. Its best to choose a class in your app that doesnthave any subclasses. You replace the    .m and    .h files for that class with a single    .swift file. Everything from your interface and implementation goes directly into this single Swift file. You wont create a header file. Xcode generates a header automatically in case you need to reference it.
Online tools are also available which convert your objective-c code into Swift.

The Cons of Swift Programming Language –

*The language is quite new.Si it has still many issues which need to be fixed.

* As it introduces new features and modifications in every release it is not yet stable.

References :

1) https://developer.apple.com/swift/
2) https://careerfoundry.com/en/blog/ios-development/introduction-to-swift-for-non-programmers/
3) https://www.altexsoft.com/blog/engineering/the-good-and-the-bad-of-swift-programming-language/