Swift Tips

Curated Swift Tips (with Code Animation)

Swift is a powerful programming language that has become the go-to choice for iOS developers since its release in 2014. Swift has a clean syntax, offers better performance than its predecessor Objective-C, and allows for safer coding with its strong…

Rendering Markdown in SwiftUI

Markdown is a lightweight markup language that is widely used for formatting plain text documents, and it is a great way to add rich text formatting to your SwiftUI applications. With the power of SwiftUI, it’s easy to render Markdown…

What is given-when-then testing in Swift?

Given-When-Then (GWT) is a structured way to write unit tests. I find it is particularly useful when I need to write complex unit tests. The structure helps me to write and understand unit test faster than the traditional approach. Using this…

What does the associated type mean in Swift 5+?

The associated type in Swift gives a placeholder name to a type that will be used as part of the protocol. When the protocol is conformed, the actual type to use for that it is specified. The associated type makes the protocol generic by…

How to use defer in Swift?

Using defer keyword in Swift code is uncommon. But, It is a very powerful concept for certain use case. The code block inside defer statement is executed just before transferring program control. It does not matter how program control is…

How to use inout parameters?

All function parameters in Swift are constants by default. That means you can not change the value of the parameters. Trying to change the value of a function parameter from within the body of that function results in a compile-time…

How to use DispatchGroup in Swift?

DispatchGroup allows you to add a set of tasks and schedule them for asynchronous execution. This behaviour can be helpful when progress can’t be made until all of the specified tasks are complete. For instance, If you have several long-running…

Optional methods in protocols

In Swift, If you conform to a protocol you need to implement all its methods or properties. Optional methods are not allowed when implementing protocols. For instance: We can try some tricks to make the optional methods. Use default implementation…

Use of final keyword in method and class

The Final is a class modifier that prevents it from being inherited or being overridden. While writing an API developer might have a class in the framework that can be abused or misused when subclassed. The solution will be the…

How to use Variadic Parameters in Swift 5+?

A variadic parameter accepts zero or more values of a specified type. When creating API it seems more developer-friendly as we don’t needs to generate an array at the caller site in order to pass multiple arguments. The variadic parameter using three…

When to use computed properties over methods?

I prefer to use computed properties when I need to compose new data from existing data sources or I want to reduce a complicated, nested property name to a more readable and easy to use one (but update it when…