Photo by James Harrison on Unsplash

Closures are a difficult topic to grasp in Swift, but they are simple to grasp if well described. Closures are functional blocks that can be used or passed around in code.

Closures are an old term that is identical to C and Objective-C blocks. Closures are similar to functions, but they are more flexible and have a more streamlined appearance. Closures are nested functions, and they can capture value, as we’ll see later in this article. Closures have the following significant characteristics:


The cleaner your code is, the easier it is to maintain

Photo by Clément Hélardot on Unsplash

When your project becomes more complex, you’ll need to write more stable and simple code. Every line of code you write is important because the cleaner your code is, the easier it is to maintain.

We’ll use the guard statement to help handle control flow in this post. The provided condition in the guard statement must be valid in order for the code to be executed after the guard statement. If the given condition is false, the guard statement’s else block will be executed.

Basic Expression

guard condition else {
//execute some code when statement is false
}
//execute some code…


Photo by Charles Deluvio on Unsplash

When creating an app, bear in mind that it is important to engage users and solicit input in order to enhance the app’s experience. Your app’s feedback and ratings will appear on the App Store, helping to boost sales and ensuring your app’s popularity.

From iOS 10.3, Apple implements a standard interface for implementing ratings and reviews, which is used to solicit user input through a built-in mechanism. It offers a way for users to connect with each other, which will be very useful in future app versions. …


Photo by Mario Gogh on Unsplash

You built your iOS app after lot of hard work and now is the time to list your app on App Store. When you list your app on App Store you need screenshots which shown on App Store. These screenshots plays huge role on app download.

When you upload screenshots make sure that iPhone screenshots must be capture in iPhone it shouldn’t taken in iPad and the same will apply on iPad screenshots otherwise your app will be rejected by App Store and shows status to “metadata rejected”.

Your screenshots must show core concept of your application. Because screenshots helps…


Photo by Nikola Majksner on Unsplash

In computer science, there are two types of algorithms commonly used to solve a problem searching and sorting algorithms. Its valuable to know which algorithm to use to perform a computation.In this tutorial, we will focus on searching algorithms.

When we talk about searching algorithms the use of binary search algorithm is very common. It is a very simple algorithm used to find a value within a sorted array.How binary search algorithm works?

How binary search algorithm works?

In binary search, we find specific value in sorted array by comparing that value to mid element of array. If value matches the mid element of array…


Photo by Kaleidico on Unsplash

It’s a good practice to know how memory management works in Swift. Swift uses Automatic Reference Counting (ARC) to manage the app’s memory automatically without writing any additional code. It deallocates the memory of instances when it is no longer used. But sometimes we have to provide more information to avoid memory leaks and deadlocks.

ARC only applies on instance of classes because classes are reference type. It didn’t applies on structures and enumerations because they are value type.

By default, class instance creates a strong reference with their methods, constants, variables, etc. means it will not be deallocated until…


Photo by Marvin Meyer on Unsplash

Unit testing is a testing method where you can test “unit” of code whether it is working as you want or not. In Xcode, use XCTest framework to perform unit tests.

Unit test is a function starts with lowercase word "test” and it must be method of subclass of XCTestCase . It has conditions to check code is doing right as expected, but it has no parameters and no return value.

How to setup unit testing

Because units tests are perform under unit testing target you must have to add them before use. …


Photo by Christopher Gower on Unsplash

Structures and Classes are basic templates for any application which consists of properties and methods implements for behaviour . You can define structure or class in a single file and there are lot of similarities between both structures and classes like defining properties, methods, initializers etc.

The object refer to class in another languages is called instance of class in Swift.

Difference between Structures and Classes


Photo by Christine Donaldson on Unsplash

Swift provides a very useful feature which is to define your own custom operators. It is a very confusing topic in the beginning but it is very powerful to use. It increases our code readability and provides a very clear and concise way to write code.

You can declare custom operators by defining static type method inside an extension. First of all, you have to decide your operator type that is it prefix, infix or postfix.

Prefix Operators

You can declare prefix operator with prefix modifier before the func keyword.

extension String {
static prefix func !! …


Photo by Brian Kyed on Unsplash

A protocol can defines a set of methods that can be adopted by any class, but we can’t write code inside.On the other hand, extensions gives us the power to write code inside methods, but it can only used for one type.

In Protocol Extensions, we can extend protocols to provide implementations to all conforming types rather than extending a particular type.

For example, we have an array named oddNumbers contains odd numbers and a Set named evenNumbers that contains even numbers.

let oddNumbers: [Int] = [1,5,23,91]let evenNumbers: Set<Int> = [34,56,12,78,22]

Now, we have protocol Collection that can be…

Gurjit Singh

I’m Computer Science graduate and an iOS Engineer who writes about Swift and iOS development. Follow me on twitter @gurjitpt and for more articles www.gurjit.co

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store