Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community, first released in 2014.Swift was developed as a replacement for Apple’s earlier programming language Objective-C, as Objective-C had been largely unchanged since the early 1980s and lacked modern language features. Swift works with Apple’s Cocoa and Cocoa Touch frameworks,
Apple announced Swift 1.0 in 2014. The new programming language came with features to accelerate development of applications for iOS and macOS. Apple promoted Swift as a modern, safe, fast, and interactive programming language. Despite being promoted as a modern alternative to Objective-C, Swift was fully compatible with Objective-C. The developers even had option to use Swift and Objective-C code in the same app. But Swift, unlike Objective-C, enables programmers to write iOS and OS X apps without writing additional code.
Also, the programming language was much faster than Objective-C. In addition to being an object-oriented programming language, Swift also supports a number of functional programming concepts. At the same time, Swift 1.0 came with a set of new language features including variables, constants, type interface, generic classes, functions, closures, tuples and dictionaries. Apple further rolled out a completely redesigned version of XCode to accelerate Swift development. The Playground feature provided by the updated version of XCode enables developers to test and render Swift code in real-time environment.
Apple rolled out Swift as a proprietary programming language. But it subsequently made the programming language open source. In 2015, Apple announced Swift 2 as an updated and open sourced version of Swift 1. The version 2 of Swift came with additional safety features to keep the error handling process secure. The new language features included in Swift 2 also made it a protocol-oriented programming language. These features enable developers to use protocols as interfaces, extend protocols, and specify methods and properties. At the same time, Swift 2 also came with several new language features including renamed syntax, migrator and Objective-C generics.
In 2016, Apple released version 3 of Swift programming language with major code changes. Unlike its predecessors, Swift 3 required programmers to add labels to all function parameters. It further enables developers to keep the code concise by removing unnecessary words from function names. The programmers have to use lowerCamelCase instead of UpperCamelCase while naming a class, struct, enum, or property. Swift 3 made it easier for programmers to import C functions into the code by introducing new attributes for C functions. Apple further changed the Swift API Guidelines to make it easier for developers to describe concepts using verbs and nouns.
In 2017,Apple plans to release Swift 4 in the fall of 2017. But the company has already released bête versions of Swift 4 to developers for testing. Swift 4 will come with XCode 9. Apple further keeps the Swift 3 to Swift 4 migration process simple. The developers can use the migration tool provided by XCode 9 to move their apps from Swift 3 to Swift 4 seamlessly. However, Swift 4 will come with a set of new and improved APIs — strings, set, dictionary, archival and serialization. Also, it will enable programmers to create multi-string literals using simple syntax. The new Codable protocol introduced by Swift 4 help programmers to serialize and deserialize custom data types without writing complex code. The updated version of XCode will add several new functionalities to Swift 4.
In 2019,Added the Extended String Delimiters section and updated the String Literals section with information about extended string delimiters.Added the dynamicCallable section with information about dynamically calling instances as functions using the dynamicCallable attribute.Added the unknown and Switching Over Future Enumeration Cases sections with information about handling future enumeration cases in switch statements using the unknown switch case attribute.Added information about the identity key path (.self) to the Key-Path Expression section.Added information about using the less than (<) operator in platform conditions to the Conditional Compilation Block section.
Swift supports closures (known as lambdas in other languages)
(arg1: Int, arg2: Int) -> Int in
return arg1 + arg2
Under the Cocoa and Cocoa Touch environments, many common classes were part of the Foundation Kit library. This included the NSString string library (using Unicode, UTF-8 in Swift 5, changed from UTF-16), the NSArray and NSDictionary collection classes, and others. Objective-C provided various bits of syntactic sugar to allow some of these objects to be created on-the-fly within the language, but once created, the objects were manipulated with object calls. For instance, in Objective-C concatenating two NSStrings required method calls similar to this:
NSString *str = @”hello,”;
str = [str stringByAppendingString:@” world”];
Swift supports five access control levels for symbols: open, public, internal, fileprivate, and private.
private : indicates that a symbol is accessible only in the immediate scope,
fileprivate : indicates it is accessible only from within the file,
internal : indicates it is accessible within the containing module,
public : indicates it is accessible from any module,
open : indicates that the class may be subclassed outside of the module.
Optionals and chaining
An important new feature in Swift is option types, which allow references or values to operate in a manner similar to the common pattern in C, where a pointer may refer to a value or may be null. This implies that non-optional types cannot result in a null-pointer error; the compiler can ensure this is not possible.
Optional types are created with the Optional mechanism—to make an Integer that is nullable. (!)
let myValue = anOptionalInstance!.someMethod()
concept of optional chaining to test whether the instance is nil and then unwrap it if it is non-nul (?)
let myValue = anOptionalInstance?.someMethod()
objects are represented internally in two parts. The object is stored as a block of data placed on the heap, while the name (or “handle”) to that object is represented by a pointer.
Objects are passed between methods by copying the value of the pointer, allowing the same underlying data on the heap to be accessed by anyone with a copy. In contrast, basic types like integers and floating-point values are represented directly.
The data is passed directly to methods by copying. These styles of access are termed pass-by-reference in the case of objects, and pass-by-value for basic types.
Protocol-Oriented Programming is a new programming paradigm ushered in by Swift. In the Protocol-Oriented approach, we start designing our system by defining protocols. We rely on new concepts: protocol extensions, protocol inheritance, and protocol compositions. In Swift, value types are preferred over classes.This combination of protocols, defaults, protocol inheritance, and extensions allows many of the functions normally associated with classes and inheritance to be implemented on value types
Libraries, runtime and development
Swift uses the same runtime as the extant Objective-C system, but requires iOS 7 or macOS 10.9 or higher. It also depends on Grand Central Dispatch.Swift and Objective-C code can be used in one program, and by extension, C and C++ also. In contrast to C, C++ code cannot be used directly from Swift. An Objective-C or C wrapper must be created between Swift and C++
Swift uses Automatic Reference Counting (ARC) to manage memory. Apple used to require manual memory management in Objective-C, but introduced ARC in 2011 to allow for easier memory allocation and deallocation.One problem with ARC is the possibility of creating a strong reference cycle, where objects reference each other in a way that you can reach the object you started from by following references. This causes them to become leaked into memory as they are never released. Swift provides the keywords weak and unowned to prevent strong reference cycles.
Debugging and other elements
A key element of the Swift system is its ability to be cleanly debugged and run within the development environment, using a read–eval–print loop (REPL), giving it interactive properties more in common with the scripting abilities of Python than traditional system programming languages. The REPL is further enhanced with the new concept playgrounds. These are interactive views running within the Xcode environment that respond to code or debugger changes on-the-fly.