iOS – Are 20 Quick Valuable Concepts Best way to Learn Swift?

Hello Readers, CoolMonkTechie heartily welcomes you in this article (Are 20 Quick Valuable Concepts Best way to Learn Swift? ).

In this article, We will discuss 20 quick valuable concepts which is the best way to learn swift and helps to build iOS applications. We’ll need Swift Foundation Concepts as we start building apps. It means that we introduce each topic in a way. This provides enough background to understand the foundation concepts.

A famous quote about learning is :

The beautiful thing about learning is that nobody can take it away from you.

So Let’s begin.


1. Hello World

It is common to start any tutorial for a new language with the Hello World example so we’ll start out by showing how easy this is to do in Swift:

print("Hello World!")      // Prints "Hello World!" to the output window in Xcode

To make this a little more personal, let’s look at one of the handy features of Swift (called string interpolation) where we can insert values into strings using the \(...) syntax. Below, we’ve inserted name into the middle of our string:

let name: String = "World"
print("Hello \(name)!")     // Prints "Hello World!"


2. When to use let vs var

We’ll notice in the example above we’ve used let to create the new variable for the name “Bob”. In Swift, we’ll choose from the following 2 options when creating a new variable:

  • let => Use let when we are defining a constant (a value that will not change)
let numberOfContinents: Int = 7   // Seven continents will not change so we've used "let"
  • var => Use var when we are defining a variable (a value that might change)
var continentsVisited: Int = 2   // Continents visited increases over time so we've used "var"

In general, it is considered a best practice to use constants (let) whenever possible.


3. Numbers

The two most common types of numbers we’ll use in Swift are integers (Int) and doubles (Double):

Integers are whole numbers with no fractional component:

let minValue: Int = -42
let maxValue: Int = 55 

Doubles can have a fractional component:

let pi: Double = 3.14159
let billAmount: Double = 10.25


4. Strings

Strings represent a series of characters:

let hello: String = "Hello"
let world: String = "World"

We can use string interpolation to construct new strings:

let helloWorld: String = "\(hello) \(world)"    // "Hello World"

Or we can use the + operator to combine strings:

let helloWorld: String = hello + " " + world    // "Hello World"


5. Booleans

Boolean is a very simple type in Swift as it can only ever be true or false:

let swiftIsCool: Bool = true
let iMissObjectiveC: Bool = false 


6. Arrays

Arrays store a list of values that must be of the same type. Below we’ve kept track of the previous bill amounts (which is a list of doubles):

// Notice how we've used "var" here since we want to append new items to the array
var previousBillAmounts: [Double] = [10.25, 21.32, 15.54]

To add a new bill to the array:

previousBillAmounts.append(52.45)    // Result: [10.25, 21.32, 15.54, 52.45]

To check on how many bills there are in the array:

let count = previousBillAmounts.count            // Result: 4

Or to check the first bill amount in the array:

let firstBillAmount = previousBillAmounts[0]    // Result: 10.25


7. Dictionaries

Much like we might use a real-world dictionary to look up the definition for a word, you can use the dictionary data structure in Swift to create associations between keys (the word in the real-world dictionary) and values (the definition in the real-world dictionary).

For example, let’s say we want to keep track of the ages of people that are using our app. This will best done using the following dictionary:

var people: [String: Int] = [
                              "Bob": 32,
                              "Cindy": 25
                            ]

Here, the key is the name of the person and the value is the person’s age.

Later on if we want to find out Bob’s age, we can look it up by doing:

let bobsAge = people["Bob"]

If we get a new user we can easily add them to our dictionary using the following syntax:

people["Dan"] = 56


8. Specifying Types

In most of the examples in this article, we are explicit with the types of our constants and variables. Explicit typing looks something like:

let name: String = "Bob"   // Explicitly designating "name" to be of type "String"

However, Swift is smart enough to infer the type for us in a lot of cases. The short example of previous example is:

let name = "Bob"   // Swift infers "name" is of type "String" since "Bob" is a String

In cases where Swift can infer the type, it’s not necessary to be explicit with the types for our constants and variables. We just do it in this guide so that the examples are easier to follow.


9. Any and AnyObject

Swift has two special “catch all types” that come in handy when a more specific type cannot determine.

  • AnyObject can represent an instance of any class type.
  • Any can represent an instance of any type at all.

In general, it’s a best practice to be as specific with our types as possible and avoid the use of AnyObject and Any, but they become particularly helpful when interacting with Objective-C code that is less strict on typing.


10. Optionals

Optionals is a very important concept in Swift and it means to improve the safety of Swift code. By simply placing a question mark (?) after any type, it declares that variable to be optional. An optional type allows that variable to exist in one of the following two states:

  1. There is a value and it equals x
  2. There isn’t a value at all

Let’s look at an example to make this more clear. Consider the following 2 examples where we are trying to convert a String to an Int:

// Example 1 (Conversion succeeds)
let input: String = "123"
let optionalConvertedInput: Int? = Int(input)  // optionalConvertedInput = 123

// Example 2 (Conversion fails - input is not a number)
let input: String = "123abc"
let optionalConvertedInput: Int? = Int(input)  // optionalConvertedInput = nil

Swift requires optionalConvertedInput to be of type Int? (or “optional Int”) so that it is explicit that convertedInput might not contain a value (in the case when the conversion fails). If we were to declare convertedInput as simply Int, we’d get a compile error.

There’s a handy syntax in Swift that we’ll use quite often when working with optionals. If we wanted to use the value of optionalConvertedInput later on in our code, we’d have to first check to make sure it’s not nil. We can do so using the following code:

if let convertedInput = optionalConvertedInput {
   // Code that gets executed when optionalConvertedInput is NOT nil
} else {
   // OptionalConvertedInput IS nil, do something else
}

In other languages like Java, NullPointerException is a common source of crashes. This exception fires when a null reference accesses. Swift’s Optionals go a long way to reduce this type of programming error.


Optionals in string interpolation

When using optionals in string interpolation, the value with either be Optional(...) or nil without being explicitly unwrapped. When the inner value needs, the optional must unwrap.

let maybeString: String? = "value"

print("\(maybeString)")  // prints -> Optional("value")
print("\(maybeString!)")  // prints -> "value"


11. Functions

Functions in Swift are very similar to other languages. The simplest function in Swift can write as:

// This prints "Hello!" to the output window
func printHello() {
    print("Hello!")
}

// Calls this function
printHello()

We can extend this function to be a little more personable by taking in a name and returning the greeting instead of printing it out:

// Takes in a "personName" parameter of type String and returns the greeting as a String
func sayHello(personName: String) -> String {
    // Implicitly returns when there is a single statement
    "Hello \(personName)!"
}

// Calls this function
let greeting: String = sayHello("Bob")

Things get a little more interesting when we start to have multiple parameters as Swift has the concept of external and local parameter names. An external parameter name is used to label arguments passed to a function call. A local parameter name is a name used in the implementation of the function.

// "to" + "and" are the external parameter names
// "person" + "anotherPerson" are the local parameter names
func sayHello(to person: String, and anotherPerson: String) -> String {
    "Hello \(person) and \(anotherPerson)!"
}

// Calls this function using the "external" parameter names
let greeting: String = sayHello(to: "Bill", and: "Ted")


12. Control Flow


Conditional Statements

If statements are very similar to other languages and can express as:

let temperatureInFahrenheit: Int = 90

if temperatureInFahrenheit <= 32 {
    print("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
    print("It's really warm. Don't forget to wear sunscreen.")
} else {
    print("It's not that cold. Wear a t-shirt.")
}


Loops

The two most common types of loops we’ll need in Swift are for loops and for-in loops.

For loops work well when we want to do something until a particular condition is met (in the case below until index >= 3):

// Simple for loop that prints "Hello" 3 times
for var index = 0; index < 3; index++ {
    print("Hello")
}

C-style for loops deprecated and will remove in future versions of Swift. For-in loops are preferred instead. The above example could be re-written as:

for _ in 0..<3 {
          print("Hello")
      }

For-in loops come in really handy when we want to do something to each item in a collection (such as an array):

let names = ["Anna", "Alex", "Brian", "Jack"]

// Loops over each name in "names" and prints out a greeting for each person
for name in names {
    print("Hello, \(name)!")
}

Sometimes, we want to loop over each item in an array and also keep track of the index of the item. Array’s enumerated() method can help you achieve this:

let names = ["Anna", "Alex", "Brian", "Jack"]

for (index, value) in names.enumerated() {
    print("Item \(index + 1): \(value)")
}


13. Classes

Classes are the building blocks of our app’s code. We define properties and methods to add functionality to our classes by using the same syntax as for variables and functions.

Below we can find a Person class that is meant to show an example of the types of things we’ll want to do when building our classes.

class Person {
	
    // Custom initializer - takes in firstName and lastName
    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName

        // Increment type property each time a new person is created
        Person.numberOfPeople++
    }
	
    // *** Properties ***
	
    // Stored Property - Stored as part of the current instance of the class
    var firstName: String
    var lastName: String

    // Computed Property - computes "fullName" from "firstName" and "lastName"
    var fullName: String {
    	get {
            "\(firstName) \(lastName)"
    	}
    }

    // Type Property - Single instance for all instances of the class,
    // similar to a static property in Java
    static var numberOfPeople = 0   
    
    // *** Methods ***
    
    // Instance Method
    func greet() {
    	// Notice the use of "self" - self refers to the current instance and 
        // is similar to "this" in Java
    	print ("Hello \(self.firstName)")
    }
    
    // Type Method
    class func printNumberOfPeople() {
    	print("Number of people = \(Person.numberOfPeople)")
    }
}

// ... Using the Person Class ...

// Create a new instance of the Person class
let bob = Person(firstName: "Bob", lastName: "Smith")

// Call instance method
bob.greet()   // Prints "Hello Bob"

// Accessing properies
print("Bob's first name is: \(bob.firstName)")  // Prints "Bob's first name is: Bob"
print("Bob's full name is: \(bob.fullName)")    // Prints "Bob's full name is: Bob Smith"

// Call type method
// Prints "Number of people = 1" (since we've only created one Person)
Person.printNumberOfPeople()


14. Protocols

Protocols are similar to interfaces in other languages. Think about a protocol as a contract. The contract includes a set of methods that must be implemented. Any classes that choose to implement a protocol sign this contract and implement all the methods that are in the protocol.

Let’s say we have the following protocol (MyFriendlyGreeterProtocol) that defines 2 methods sayHello() and sayBye():

protocol MyFriendlyGreeterProtocol {
    func sayHello()
    func sayBye()
}

Any classes that implement this protocol (you implement a protocol by adding its name after the class defintion as shown below), must implement both of these methods:

class MyEnglishPerson: MyFriendlyGreeterProtocol {
    func sayHello() {
        print("Hello!")
    }

    func sayBye() {
        print("Bye!")
    }
	
    // ... other methods for this class ...

}

class MySpanishPerson: MyFriendlyGreeterProtocol {
    func sayHello() {
        print("Hola!")
    }

    func sayBye() {
        print("Adios!")
    }
	
    // ... other methods for this class ...
}

There is a lot more you can do with protocols as they form one of the key design patterns in iOS. The code above merely shows how to get started with the syntax for protocols.


15. Swift Closures

Closures are self-contained blocks of code that can be passed around and used in our code. These are similar to blocks in Objective-C and lambdas in other programming languages.

Swift Closures can capture and store references to any constants and variables from the context in which they are defined. This is known as “closing” over those constants and variables. Swift handles all of the memory management of capturing for you.

Closure Example:

Example: A function called makeIncrementer which contains a nested function

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 increments runningTotal by amount each time it is called.

func makeIncrementer(forIncrement amount: Int) -> () -> Int {
    var runningTotal = 0
    func incrementer() -> Int {
        runningTotal += amount
        return runningTotal
    }
    return incrementer
}

The return type of makeIncrementer is () -> Int. This means that it returns a function, rather than a simple value. The function it returns has no parameters, and returns an Int value each time it is called.

The makeIncrementer(forIncrement:) function defines an integer variable called runningTotal, to store the current running total of the incrementer that will be returned. This variable is initialized with a value of 0.

The makeIncrementer(forIncrement:) function has a single Int parameter with an external name of forIncrement, and a local name of amount. The argument value passed to this parameter specifies how much runningTotal should be incremented by each time the returned incrementer function is called.

makeIncrementer defines a nested function called incrementer, which performs the actual incrementing. This function simply adds amount to runningTotal, and returns the result.

When considered in isolation, the nested incrementer() function might seem unusual:

func incrementer() -> Int {
    runningTotal += amount
    return runningTotal
}

The incrementer() function doesn’t have any parameters, and yet it refers to runningTotal and amount from within its function body. It does this by capturing a reference to runningTotal and amount from the surrounding function and using them within its own function body. Capturing by reference ensures that runningTotal and amount do not disappear when the call to makeIncrementer ends, and also ensures that runningTotal is available the next time the incrementer function is called.

Example: a function called makeIncrementer in action

Here’s an example of makeIncrementer in action:

// This example sets a constant called incrementByTen to refer to an //incrementer function that
// adds 10 to its runningTotal variable each time it is called.

let incrementByTen = makeIncrementer(forIncrement: 10)

incrementByTen()  // returns a value of 10
incrementByTen()  // returns a value of 20
incrementByTen()  // returns a value of 30


16. Type Casting (as, as?, as!)

Type casting changes the type of a particular instance to another compatible type. There are 3 ways to accomplish this with Swift:

  1. Guaranteed conversion with as : This is the safest cast. It will never fail since the compiler can guarantee the cast will work. Use this when you are upcasting from a child class to its parent or doing something like 1 as Float.
// Guaranteed conversion as the compiler can verify this will succeed
let myFloat = 1 as Float

// Guaranteed conversion as upcasting from a type to its parent type is safe 
// UIView is a parent of UITableView
let myView = myTableView as UIView

2. Conditional conversion with as? : This is a cautious cast. If the cast fails, it will return nil. This is needed when downcasting from a parent type to a child type.

// If myView is actually a tableView, the downcast will succeed, otherwise it will fail safely
if let myTableView = myView as? UITableView {
    print("The downcast succeeded!")
} else {
    print("The downcast failed!")   	   
}

3. Forced conversion with as! : This is a dangerous cast that you should avoid using. If the cast fails, this will crash our app. Use this cast carefully.

// DANGEROUS: If myView is actually a tableView, the downcast will succeed
// Otherwise it will crash the app
let myTableView = myView as! UITableView


17. Understanding the Exclamation Mark (!)

There are various places you might come across an exclamation mark in Swift code. The following examples are meant to capture the major types of use cases for the exclamation mark operator that can cause confusion when first learning Swift.

When getting the actual value out of an optional (called unwrapping an optional):

let possibleString: String? = "An optional string."

// DANGEROUS: possibleString must NOT be nil or this will crash
let forcedString: String = possibleString! 

// SAFE: Will only enter the if clause if possibleString is NOT nil
if let actualString = possibleString {
    // do something with actualString
}

// SAFE: Generally preferred alternate syntax to "if let" that can exit early
guard let actualString = possibleString else {
    // exit early or throw exception
}
// do something with actualString

When type casting (called forced conversion):

// DANGEROUS: If myView is actually a tableView, the downcast will succeed
// Otherwise it will crash the app
let myTableView = myView as! UITableView

When defining variables that are initially nil but get set soon afterwards and are guaranteed not to be nil after that (called implicitly unwrapped optionals):

let assumedString: String! = "An implicitly unwrapped optional string."

// no need for an exclamation mark since assumedString is an implicitly unwrapped optional
let implicitString: String = assumedString


18. Property Observers

A very useful inclusion of the Swift language is Property Observers. While the language might sound complex, it’s actually a literal explanation of what it does. It allows you to observe properties and respond to impending or finished property changes. It’s part of Apple’s goal to make Swift a cleaner language because a developer merely has to keep any logic related to updating properties in one convenient place.

Let’s look at an example.

Assume we have a Fitness tracking application and whenever a user enters their weight, we calculate and display their BMI to them.

Without property observers, and keeping in good practice (since BMI is calculated and not set in initialization), we might have a class like this –

Swift

class Profile {
    let height: Double
    var weight: Double
    private var BMI: Double
    
    init(weight: Double, height: Double) {
        self.height = height
        self.weight = weight
        self.BMI = weight / (height*height)
    }
    
    func getBMI() -> Double {
        self.BMI
    }
    
    func updateBMI() {
        self.BMI = weight / (height*height)
    }
}  

In a view where we ask the user to enter their new weight, we would call profile.updateBMI() after the new weight is set. Isn’t this a little tedious? It’s almost a trap for developers. We have to actively remember that we should update our BMI every time we update our weight.

Thankfully, with property observers, we don’t have to actively watch every property change as a developer. Our class becomes –

class Profile {
    let height: Double
    var weight: Double {
        didSet {
            updateBMI()
        }
    }
    private var BMI: Double
    
    init(weight: Double, height: Double) {
        self.height = height
        self.weight = weight
        self.BMI = weight / (height*height)
    }
    
    func getBMI() -> Double {
        self.BMI
    }
    
    func updateBMI() {
        self.BMI = weight / (height*height)
    }
}  

It might not look like much, but now whenever we update BMI, we don’t have to worry about calling updateBMI() and to be honest, we can clean this up even more by making BMI private(set), changing Profile to a struct and adding a mutating func.

struct Profile {
    let height: Double
    var weight: Double {
        didSet {
            updateBMI()
        }
    }
    private(set) var BMI: Double
    
    mutating private func updateBMI() {
        BMI = weight / (height * height)
    }
}  

What is mutating we ask? Well, normally Struct’s are read only. This is because in order for them to be fast, they need to take up space on Stack instead of Heap. A simple way to explain this is to imagine that Stack is writing with a Pen and Heap is writing with a Pencil. Some of you might think “well what’s the big deal? they both write.” But think about all the extra work that comes with a Pencil – We need an eraser, we have to clean the eraser off the paper and can end up using more material if we edit a lot. A pen, on the other hand, we just write (we’re assuming you’re not using an erasable pen or white-out) and it’s set.

Mutating gives us the ability to say, “Hey, this Struct will be read-only, but if I make it a variable, I might want to update its properties, so please give me the ability to edit that Struct”.

There’s a little more to this under the hood, but Structs tend to be pretty thread-safe, so we can assume every Struct exists by itself and it’s pretty safe to edit like this.


Other Property Observers

There are other property observers for every stage of a state change – willSet, set/get are included. willSet has the added ability of being able to compare newValue to the original value (called the property name).

A few hints about where didSet can be quite powerful is when dealing with network data and data sources. When a user has the ability to get objects from a server with a regular network call, a search and tapping a keyword, it can be useful if the var dataSource: [Models] has didSet to reload the data of the view. That way we won’t have to do it manually.


19. Working with JSON

A lot of the time when working with REST API’s (like Instagram, Twitter, etc), the data that comes back will be JSON. JSON is a human readable data format (very similar to XML).

Below is an example of JSON that simulates the type of JSON. We might get back when using an endpoint that returns movies and their ratings:

  • An open curly brace means the start of a dictionary
  • An open bracket means the start of an array
{
    "status": "OK",
    "movies": [
        {
            "title": "Whiplash",
            "rating": 8.5
        }, 
        {
            "title": "Feast",
            "rating": 5.2
        }, 
        {
            "title": "Kung Fury",
            "rating": 7.1
        }
    ]
}


Below is an example of how we extract the movies and ratings from that response:

// data returned from the network response will typically be of type
// NSData (which is a buffer of bytes)
let responseData: NSData = // ... some value retrieved from the network response ...

// Wrap our code in a do catch as our code might throw an exception which we need to handle
do {
    // Start by converting the NSData to a dictionary - a dictionary for the entire response
    if let responseDictionary = try NSJSONSerialization.JSONObjectWithData(responseData,
        options:NSJSONReadingOptions(rawValue:0)) as? [String:AnyObject] {

            // Dip inside the response to find the "movies" key and get the array of movies
            if let movies = responseDictionary["movies"] as? [AnyObject] {

                // Get each movie dictionary from the array of movies
                for movie in movies {
                
                    // Use the movie "title" key and "rating" key to get their values
                    if let title = movie["title"] as? String {
                        if let rating = movie["rating"] as? Double {
                            print("Title:\(title), rating:\(rating)")
                        }
                    }
                }
            }
    }
} catch {
    print("Error parsing JSON")
}


20. Playgrounds

Xcode includes a very useful tool for learning Swift called “Playgrounds”. It’s very easy to create a new playground through Xcode.

Once inside a playground, we can write Swift code and see it run immediately (without needing to build and run a project each time). This allows you to try out different syntaxes and test out our code before including it in our app.

We highly recommend checking out Playgrounds while we are learning Swift.

That’s all about in this article.

Related Other Articles / Posts


Conclusion

In this article, We discussed 20 quick valuable concepts which are the best way to learn swift and help to build iOS applications.

Thanks for reading! I hope you enjoyed and learned about the 20 quick valuable Swift Foundation Concepts. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow other website and tutorials of iOS as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

iOS – An Overview Of Swift Closures In iOS

Hello Readers, CoolMonkTechie heartily welcomes you in this article (An Overview Of Swift Closures In iOS) .

In this article, we will learn about Swift Closures in iOS. Swift Closures are other types of Swift functions which can be defined without using keyword func and a function name. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages. This article covers Swift Closures related concepts (like Closure Expressions, Trailing Closures, Capturing Values, Closure as Reference Types and Auto-closures) in iOS with an authentic example.

A famous quote about learning is :

” Develop a passion for learning. If you do, you will never cease to grow. ”

So Let’s begin.

Swift Closures Overview

Closures are self-contained blocks of functionality that can be passed around and used in our code. It is similar to blocks in C and Objective-C and to lambdas in other programming languages.”

Swift Closures can capture and store references to any constants and variables from the context in which they are defined. We know this as closing over those constants and variables. Swift handles all the memory management of capturing for us.

Global and nested functions are special cases of closures. Closures take one of three forms:

  • Global functions are closures that have a name and don’t capture any values.
  • Nested functions are closures that have a name and can capture values from their enclosing function.
  • Closure expressions are unnamed closures written in a lightweight syntax that can capture values from their surrounding context.

Swift’s closure expressions have a clean and clear style, with optimizations that encourage brief, clutter-free syntax in common scenarios. These optimizations include:

  • Inferring parameter and return value types from context
  • Implicit returns from single-expression closures
  • Shorthand argument names
  • Trailing closure syntax

Swift Closure Expressions

Closure expressions are a way to write inline closures in a brief, focused syntax. Closure expressions provide several syntax optimizations for writing closures in a shortened form without loss of clarity or intent. The closure expression examples below illustrate these optimizations by refining a single example of the sorted(by:) method over several iterations, each of which expresses the same functionality in a more succinct way.

The Sorted Method

Swift’s standard library provides a method called sorted(by:), which sorts an array of values of a known type, based on the output of a sorting closure that we provide. Once it completes the sorting process, the sorted(by:) method returns a new array of the same type and size as the old one, with its elements in the correct sorted order. The original array is not modified by the sorted(by:) method.

The closure expression examples below use the sorted(by:) method to sort an array of String values in reverse alphabetical order. Here’s the initial array to be sorted:

let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]

The sorted(by:) method accepts a closure that takes two arguments of the same type as the array’s contents, and returns a Bool value to say whether the first value should appear before or after the second value once the values are sorted. The sorting closure needs to return true if the first value should appear before the second value, and false otherwise.

This example is sorting an array of String values, and so the sorting closure needs to be a function of type (String, String) -> Bool.

One way to provide the sorting closure is to write a normal function of the correct type, and to pass it in as an argument to the sorted(by:) method:

func backward(_ s1: String, _ s2: String) -> Bool {
    return s1 > s2
}
var reversedNames = names.sorted(by: backward)
// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]

In this example, if the first string (s1) is greater than the second string (s2), the backward (_:_:) function will return true, it is showing that s1 should appear before s2 in the sorted array. For characters in strings, “greater than” means “appears later in the alphabet than”. This means that the letter (B) is greater than the letter (A), and the string (Tom) is greater than the string (Tim). This gives a reverse alphabetical sort, with (Barry) being placed before (Alex), and so on.

However, this is a rather long-winded way to write what is essentially a single-expression function (a > b). In this example, it would be preferable to write the sorting closure inline, using closure expression syntax.

Closure Expression Syntax

Closure expression syntax has the following general form:

{ (parameters) -> return type in
    statements
}

The parameters in closure expression syntax can be in-out parameters, but they can’t have a default value. Variadic parameters can be used if we name the variadic parameter. Tuples can also be used as parameter types and return types.

The example below shows a closure expression version of the backward(_:_:) function from above:

reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in
    return s1 > s2
})

We are aware that the declaration of parameters and return type for this inline closure is identical to the declaration from the backward(_:_:) function. In both cases, it is written as (s1: String, s2: String) -> Bool. However, for the inline closure expression, the parameters and return type are written inside the curly braces, not outside of them.

The start of the closure’s body is introduced by the in keyword. This keyword indicates that the definition of the closure’s parameters and return type has finished, and the body of the closure is about to begin.

Inferring Type From Context

Because the sorting closure is passed as an argument to a method, Swift can infer the types of its parameters and the type of the value it returns. The sorted(by:) method is being called on an array of strings, so its argument must be a function of type (String, String) -> Bool. This means that the (String, String) and Bool types don’t need to be written as part of the closure expression’s definition. Because all of the types can be inferred, the return arrow (->) and the parentheses around the names of the parameters can also be omitted:

reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )

It is always possible to infer the parameter types and return type when passing a closure to a function or method as an inline closure expression. As a result, we never need to write an inline closure in its fullest form when the closure is used as a function or method argument.

In the case of the sorted(by:) method, the purpose of the closure is clear from the fact that sorting is taking place, and it is safe for a reader to assume that the closure is likely to be working with String values, because it is assisting with the sorting of an array of strings.

Implicit Returns from Single-Expression Closures

Single-expression closures can implicitly return the result of their single expression by omitting the return keyword from their declaration, as in this version of the previous example:

reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } )

Here, the function type of the sorted(by:) method’s argument makes it clear that a Bool value must be returned by the closure. Because the closure’s body contains a single expression (s1 > s2) that returns a Bool value, there’s no ambiguity, and the return keyword can be omitted.

Shorthand Argument Names

Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0$1$2, and so on.

If we use these shorthand argument names within our closure expression, we can omit the closure’s argument list from its definition, and the number and type of the shorthand argument names will be inferred from the expected function type. The in keyword can also be omitted, because the closure expression is made up entirely of its body:

reversedNames = names.sorted(by: { $0 > $1 } )

Here, $0 and $1 refer to the closure’s first and second String arguments.

Operator Methods

This is an even shorter way to write the closure expression above. Swift’s String type defines its string-specific implementation of the greater-than operator (>) as a method that has two parameters of type String, and returns a value of type Bool. This exactly matches the method type needed by the sorted(by:) method. Therefore, we can simply pass in the greater-than operator, and Swift will infer that we want to use its string-specific implementation:

reversedNames = names.sorted(by: >)

Trailing Swift Closures

If we need to pass a closure expression to a function as the function’s final argument and the closure expression is long, it can be useful to write it as a trailing closure instead. We write a trailing closure after the function call’s parentheses, even though the trailing closure is still an argument to the function.When we use the trailing closure syntax, we don’t write the argument label for the first closure as part of the function call.

A function call can include multiple trailing closures; however, the first few examples below use a single trailing closure.

func someFunctionThatTakesAClosure(closure: () -> Void) {
    // function body goes here
}

// Here's how you call this function without using a trailing closure:

someFunctionThatTakesAClosure(closure: {
    // closure's body goes here
})

// Here's how you call this function with a trailing closure instead:

someFunctionThatTakesAClosure() {
    // trailing closure's body goes here
}

The string-sorting closure from the Closure Expression Syntax section above can be written outside of the sorted(by:) method’s parentheses as a trailing closure:

reversedNames = names.sorted() { $0 > $1 }

If a closure expression is provided as the function’s or method’s only argument and we provide that expression as a trailing closure, we don’t need to write a pair of parentheses () after the function or method’s name when we call the function:

reversedNames = names.sorted { $0 > $1 }

Trailing closures are most useful when the closure is sufficiently long that it is not possible to write it inline on a single line.

Example

If a function takes multiple closures, we omit the argument label for the first trailing closure and we label the remaining trailing closures. For example, the function below loads a picture for a photo gallery:

func loadPicture(from server: Server, completion: (Picture) -> Void, onFailure: () -> Void) {
    if let picture = download("photo.jpg", from: server) {
        completion(picture)
    } else {
        onFailure()
    }
}

When we call this function to load a picture, we provide two closures. The first closure is a completion handler that displays a picture after a successful download. The second closure is an error handler that displays an error to the user.

loadPicture(from: someServer) { picture in
    someView.currentPicture = picture
} onFailure: {
    print("Couldn't download the next picture.")
}

In this example, the loadPicture(from:completion:onFailure:) function dispatches its network task into the background, and calls one of the two completion handlers when the network task finishes. Writing the function this way lets us cleanly separate the code that’s responsible for handling a network failure from the code that updates the user interface after a successful download, instead of using just one closure that handles both circumstances.

Capturing Values In Swift Closures

A closure can capture constants and variables from the surrounding context in which it is defined. The closure can then refer to and modify the values of those constants and variables from within its body, even if the original scope that defined the constants and variables no longer exists.

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.

Example

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 increments runningTotal by amount each time it is called.

Code Syntax 1: makeIncrementer() function

func makeIncrementer(forIncrement amount: Int) -> () -> Int {
    var runningTotal = 0
    func incrementer() -> Int {
        runningTotal += amount
        return runningTotal
    }
    return incrementer
}

The return type of makeIncrementer is () -> Int. This means that it returns a function, rather than a simple value. The function it returns has no parameters, and returns an Int value each time it is called.

The makeIncrementer(forIncrement:) function defines an integer variable called runningTotal, to store the current running total of the incrementer that will be returned. This variable is initialized with a value of 0.

The makeIncrementer(forIncrement:) function has a single Int parameter with an argument label of forIncrement, and a parameter name of amount. The argument value passed to this parameter specifies how much runningTotal should be incremented by each time the returned incrementer function is called. The makeIncrementer function defines a nested function called incrementer, which performs the actual incrementing. This function simply adds amount to runningTotal, and returns the result.

Code Syntax 2: incrementer() function

When considered in isolation, the nested incrementer() function might seem unusual:

func incrementer() -> Int {
    runningTotal += amount
    return runningTotal
}

The incrementer() function doesn’t have any parameters, and yet it refers to runningTotal and amount from within its function body. It does this by capturing a reference to runningTotal and amount from the surrounding function and using them within its own function body. Capturing by reference ensures that runningTotal and amount don’t disappear when the call to makeIncrementer ends, and also ensures that runningTotal is available the next time the incrementer function is called.

Code Syntax 3: makeIncrementer() function usage

Here’s an example of makeIncrementer usage is :

let incrementByTen = makeIncrementer(forIncrement: 10)

This example sets a constant called incrementByTen to refer to an incrementer function that adds 10 to its runningTotal variable each time it is called. Calling the function multiple times shows this behavior in action:

incrementByTen()
// returns a value of 10
incrementByTen()
// returns a value of 20
incrementByTen()
// returns a value of 30

If we create a second incrementer, it will have its own stored reference to a new, separate runningTotal variable:

let incrementBySeven = makeIncrementer(forIncrement: 7)
incrementBySeven()
// returns a value of 7

Calling the original incrementer (incrementByTen) again continues to increment its own runningTotal variable, and does not affect the variable captured by incrementBySeven:

incrementByTen()
// returns a value of 40

We are aware that Swift may instead capture and store a copy of a value as an optimization, if that value is not mutated by a closure, and if the value is not mutated after the closure is created.

Swift also handles all memory management involved in disposing of variables when they are no longer needed.

If we assign a closure to a property of a class instance, and the closure captures that instance by referring to the instance or its members, we will create a strong reference cycle between the closure and the instance. Swift uses capture lists to break these strong reference cycles.

Swift Closures As Reference Types

In the example above, incrementBySeven and incrementByTen are constants, but the closures these constants refer to are still able to increment the runningTotal variables that they have captured. This is because functions and closures are reference types.

Whenever we assign a function or a closure to a constant or a variable, we are actually setting that constant or variable to be reference to the function or closure.

Example

In the example above, it is the choice of closure that incrementByTen refers to that is constant, and not the contents of the closure itself.

This also means that if we assign a closure to two different constants or variables, both of those constants or variables refer to the same closure.

let alsoIncrementByTen = incrementByTen
alsoIncrementByTen()
// returns a value of 50

incrementByTen()
// returns a value of 60

The example above shows that calling alsoIncrementByTen is the same as calling incrementByTen. Because both of them refer to the same closure, they both increment and return the same running total.

Escaping Swift Closures

A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When we declare a function that takes a closure as one of its parameters, we can write @escaping before the parameter’s type to indicate that the closure is allowed to escape.

One way that a closure can escape is by being stored in a variable that’s defined outside the function.

Example

As an example, many functions that start an asynchronous operation take a closure argument as a completion handler. The function returns after it starts the operation, but the closure isn’t called until the operation is completed—the closure needs to escape, to be called later.

Code Syntax 1 : someFunctionWithEscapingClosure() function

var completionHandlers = [() -> Void]()
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
    completionHandlers.append(completionHandler)
}

The someFunctionWithEscapingClosure(_:) function takes a closure as its argument and adds it to an array that’s declared outside the function. If we didn’t mark the parameter of this function with @escaping, we would get a compile-time error.

An escaping closure that refers to self needs special consideration if self refers to an instance of a class. Capturing self in an escaping closure makes it easy to accidentally create a strong reference cycle.

Normally, a closure captures variables implicitly by using them in the body of the closure, but in this case we need to be explicit. If we want to capture self, write self explicitly when we use it, or include self in the closure’s capture list. Writing self explicitly lets we express our intent, and reminds us to confirm that there isn’t a reference cycle.

Code Syntax 2 : doSomething() function

For example, in the code below, the closure passed to someFunctionWithEscapingClosure(_:) refers to self explicitly. In contrast, the closure passed to someFunctionWithNonescapingClosure(_:) is a non-escaping closure, which means it can refer to self implicitly.

func someFunctionWithNonescapingClosure(closure: () -> Void) {
    closure()
}

class SomeClass {
    var x = 10
    func doSomething() {
        someFunctionWithEscapingClosure { self.x = 100 }
        someFunctionWithNonescapingClosure { x = 200 }
    }
}

let instance = SomeClass()
instance.doSomething()
print(instance.x)
// Prints "200"

completionHandlers.first?()
print(instance.x)
// Prints "100"

Here’s a version of doSomething() that captures self by including it in the closure’s capture list, and then refers to self implicitly:

class SomeOtherClass {
    var x = 10
    func doSomething() {
        someFunctionWithEscapingClosure { [self] in x = 100 }
        someFunctionWithNonescapingClosure { x = 200 }
    }
}

If self is an instance of a structure or an enumeration, we can always refer to self implicitly. However, an escaping closure can’t capture a mutable reference to self when self is an instance of a structure or an enumeration. Structures and enumerations don’t allow shared mutability.

struct SomeStruct {
    var x = 10
    mutating func doSomething() {
        someFunctionWithNonescapingClosure { x = 200 }  // Ok
        someFunctionWithEscapingClosure { x = 100 }     // Error
    }
}

The call to the someFunctionWithEscapingClosure function in the example above is an error because it’s inside a mutating method, so self is mutable. That violates the rule that escaping closures can’t capture a mutable reference to self for structures.

Auto-closures

An auto-closure is a closure that is automatically created to wrap an expression that’s being passed as an argument to a function. It doesn’t take any arguments, and when it’s called, it returns the value of the expression that’s wrapped inside of it. This syntactic convenience lets us omit braces around a function’s parameter by writing a normal expression instead of an explicit closure. It’s common to call functions that take auto-closures, but it’s not common to implement that kind of function.

For example, the assert(condition:message:file:line:) function takes an auto-closure for its condition and message parameters; its condition parameter is evaluated only in debug builds and its message parameter is evaluated only if condition is false.

An auto-closure lets us delay evaluation, because the code inside isn’t run until we call the closure. Delaying evaluation is useful for code that has side effects or is computationally expensive, because it lets us control when that code is evaluated.

Code Syntax 1 : Closure delays evaluation

The code below shows how a closure delays evaluation.

var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
print(customersInLine.count)
// Prints "5"

let customerProvider = { customersInLine.remove(at: 0) }
print(customersInLine.count)
// Prints "5"

print("Now serving \(customerProvider())!")
// Prints "Now serving Chris!"
print(customersInLine.count)
// Prints "4"

Even though the first element of the customersInLine array is removed by the code inside the closure, the array element isn’t removed until the closure is actually called. If the closure is not called, the expression inside the closure is not evaluated. It means the array element is not removed. We are aware that the type of customerProvider is not String but () -> String — a function with no parameters that returns a string.

Code Syntax 2 : Closure as an argument to a function

We can get the same behavior of delayed evaluation when we pass a closure as an argument to a function.

// customersInLine is ["Alex", "Ewa", "Barry", "Daniella"]
func serve(customer customerProvider: () -> String) {
    print("Now serving \(customerProvider())!")
}
serve(customer: { customersInLine.remove(at: 0) } )
// Prints "Now serving Alex!"

The serve(customer:) function in the listing above takes an explicit closure that returns a customer’s name. The version of serve(customer:) below performs the same operation but, instead of taking an explicit closure, it takes an auto-closure by marking its parameter’s type with the @autoclosure attribute. Now we can call the function as if it took a String argument instead of a closure. The argument is automatically converted to a closure, because the customerProvider parameter’s type is marked with the @autoclosure attribute.

// customersInLine is ["Ewa", "Barry", "Daniella"]
func serve(customer customerProvider: @autoclosure () -> String) {
    print("Now serving \(customerProvider())!")
}
serve(customer: customersInLine.remove(at: 0))
// Prints "Now serving Ewa!"

If we want an auto-closure that is allowed to escape, use both the @autoclosure and @escaping attributes.

// customersInLine is ["Barry", "Daniella"]
var customerProviders: [() -> String] = []
func collectCustomerProviders(_ customerProvider: @autoclosure @escaping () -> String) {
    customerProviders.append(customerProvider)
}
collectCustomerProviders(customersInLine.remove(at: 0))
collectCustomerProviders(customersInLine.remove(at: 0))

print("Collected \(customerProviders.count) closures.")
// Prints "Collected 2 closures."
for customerProvider in customerProviders {
    print("Now serving \(customerProvider())!")
}
// Prints "Now serving Barry!"
// Prints "Now serving Daniella!"

In the code above, instead of calling the closure passed to it as its customerProvider argument, the collectCustomerProviders(_:) function appends the closure to the customerProviders array. The array is declared outside the scope of the function, which means the closures in the array can be executed after the function returns. As a result, the value of the customerProvider argument must be allowed to escape the function’s scope.

That’s all about in this article.

Related Other Articles / Posts

Conclusion

In this article, we understood Swift Closures in iOS. This article reviewed Swift Closures related concepts (like Closure Expressions, Trailing Closures, Capturing Values, Closure as Reference Types and Auto-closures) in iOS with an authentic example.

Thanks for reading! I hope you enjoyed and learned about Swift Closures Concepts in iOS. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow other website and tutorials of iOS as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING!!???

Exit mobile version