Introduction to Swift Programming
Last Updated :
06 Feb, 2024
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 2014.
Swift 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
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:
- Protocol extensions
- Protocol inheritance
- 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++ 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.
Similar Reads
Swift Programming Language
Swift is a general-purpose, multi-paradigm, object-oriented, functional, imperative, and block-structured language. Swift is the result of the latest research on programming languages and is built using a modern approach to safety, and software design patterns by Apple Inc. for iOS applications, mac
4 min read
Swift - Function Overloading
In Swift, a function is a set of statements clubbed together to perform a specific task. It is declared using the "func" keyword. We can also set the return type of the function by specifying the data type followed by a return arrow ("->"). Syntax: func functionName(parameters) -> returnType {
8 min read
String Functions and Operators in Swift
A string is a sequence of characters that is either composed of a literal constant or the same kind of variable. For eg., "Hello World" is a string of characters. In Swift4, a string is Unicode correct and locale insensitive. We are allowed to perform various operations on the string like comparison
10 min read
Swift - Hello World Program
Swift is a Programming Language used for developing iOS-based platforms like macOS, apple mobiles, apple iOS Watches, iOS TVs, iOS Keywords, iOS Mouse, and other iOS Software-based Peripherals. This language is almost 80% similar to C and Python languages. Once who is familiar with C and Python lang
2 min read
Swift - Functions and its types
A function in Swift is a standalone section of code that completes a particular job. Code is organized into functions that can be called repeatedly throughout a program to make it simpler to understand and maintain. Here's the basic syntax for defining a function in Swift: Swift func functionName(ar
6 min read
Calling Functions in Swift
Functions are independent lumps of code that play out a particular undertaking. You can give a name to the function that recognizes what it does, and also the name is utilized to "call" the function to play out its errand when required. Or we can say that a function is a bunch of orders/explanations
6 min read
Swift - Convert String to Int Swift
Swift provides a number of ways to convert a string value into an integer value. Though, we can convert a numeric string only into an integer value. In this article, we will see the two most common methods used to convert a string to an integer value. A string is a collection of characters. Swift pr
3 min read
Non Repeating Timers in Swift
Developers may create effective and efficient applications using a number of tools with Swift, a strong and versatile programming language. The Timer class is one such tool that enables programmers to define timed events that run at predetermined intervals. In this post, we'll go over how to make a
7 min read
Swift - In Out Parameters
In Swift, a function is a collection of statements clubbed together to perform a specific task. A function may or may not contain a return type. The return type of the function decides the type of value returned by a function. For example, if we want to return an integer value from a function then t
4 min read
Swift Array joined() function
Swift support different type of generic collections and array is one of them. An array is an ordered collection of the same type of values like int, string, float, etc., you are not allowed to store the value of another type in an array(for example, in a string type of array we can only store string
3 min read