Enhancements and Features:
Let’s take a look on enhancements and new features introduced in Swift language to overcome cons of Objective-C. In Swift language , Function has come up with enhancements like ability to return multiple values , nested functions etc. Swift’s unified functions syntax is flexible enough to express anything fro simple C-style function with no parameter names to a complex Objective-C-style method with local and external parameter names for each parameter. Every function in Swift has a type, consisting of the function’s parameter types and return type, which makes it easy to pass functions as parameters to other functions, and to return functions from functions. Functions can also be written within other functions to encapsulate useful functionality within a nested function scope.
Simple example of function with multiple input parameters:
Functions can have multiple input parameters, which are written within the function’s parentheses, separated by commas.
This function takes a start and an end index for a half-open range, and works out how many elements the range contains:
func halfOpenRangeLength(start: Int, end: Int) -> Int {
return end - start
}
println(halfOpenRangeLength(1, 10))
// prints "9"
Closures: Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages. Closures can capture and store references to any constants and variables from the context in which they are defined. In Swift, the simplest form of a closure that can capture values is a nested function, written within the body of another function. A nested function can capture any of its outer function’s arguments and can also capture any constants and variables defined within the outer function, similar to how email capture tools collect user information from various forms on a website.
Here’s an example of a function called makeIncrementer, which contains a nested function called incrementer. The nested incrementer function captures two values, runningTotal and amount, from its surrounding context. After capturing these values, incrementer is returned by makeIncrementer as a closure that incrementsrunning Total by amount each time it is called.
func makeIncrementer(forIncrement amount: Int) -> () -> Int {
var runningTotal = 0
func incrementer() -> Int {
runningTotal += amount
return runningTotal
}
Enumerations: Enumerations in Swift are first-class types in their own right. They adopt many features traditionally supported only by classes. Enumerations can also define initializers to provide an initial member value; can be extended to expand their functionality beyond their original implementation; and can conform to protocols to provide standard functionality.
You introduce enumerations with the enum keyword and place their entire definition within a pair of braces:
enum SomeEnumeration {
// enumeration definition goes here
}
Multiple member values can appear on a single line, separated by commas:
enum Planet {
case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}
Generics: Generics are one of the most powerful features of Swift, and much of the Swift standard library is built with generic code. Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.
More powerful optional unwrapping with if let — The if let construct can now unwrap multiple optional at once, as well as include intervening boolean conditions. This lets you express conditional control flow without unnecessary nesting.
New native Set data structure — An unordered collection of unique elements that bridges withNSSet and provides value semantics like Array and Dictionary.
Nullability may now be expressed in Objective-C headers — New Objective-C extensions in Clang allow you to express the nullability of pointers and blocks in your Objective-C API. You can provide Objective-C frameworks that work great with Swift code, and improve your application development when mixing and matching with Objective-C code in your own project.