Swift

When we take the Test Driven Development(TDD) approach to write code we eventually ask ourselves a couple of questions: Have I covered all possible use cases? Have I written enough unit tests? What helps me is writing a bulleted list of functional requirements for the feature I am developing. Having a clear list of things…

Read More Swift TDD. How Many Unit Tests to Write?

In this short tutorial, you will learn how to set a different font on UIButton in Swift programmatically. There are lots of different customization you can with UIButton. For example, you might be also interested to learn how to create UIButton and change its style, background color, tint color and make it call a function…

Read More Set UIButton Font Programmatically in Swift

In this tutorial, you will learn how to create UIButton programmatically. You will also learn how to do the basic customization of UIButton like setting button tint color, background color and making your button call a function when tapped. Create UIButton A new UIButton can be created of different types: ButtonType.custom, ButtonType.system, ButtonType.detailDisclosure, ButtonType.infoLight, ButtonType.infoDark,…

Read More Create UIButton in Swift Programmatically

If needed, it is possible to make UILabel clickable in Swift although it is recommended to use UIButton instead. To make UILabel clickable in Swift, you will need to create a UITapGestureRecognizer and then add it to a label. In this tutorial, you will learn how to create UILabel in Swift programmatically and how to…

Read More Clickable UILabel in Swift Programmatically

When writing code, we organize functionality into functions and group functions and different properties into classes. In Swift, additionally to creating classes we can also create structures or struct for short. Although very similar, Swift classes are different from swift structures. Comparing Structures and Classes According to Swift documentation, structures and classes in Swift have many…

Read More Swift Struct Tutorial with Code Examples

In this tutorial, you will learn how to create UILabel programmatically using a specific height, width, and position as well as using an auto layout. To write Swift code I will use Xcode Playground and if you are interested to learn how to create a new Xcode Playgrounds project, have a look at this tutorial…

Read More Create UILabel Programmatically in Swift

This page contains different code snippets on how to work with Dictionary in Swift. Create Empty Dictionary // Create an empty dictionary let myDictionary = [String:String]() // Another way to create an empty dictionary let myDictionary2:[String:String] = [:] // Keys in dictionary can also be of type Int let myDictionary3 = [Int:String]()   Add Item…

Read More Working with Dictionary in Swift. Code Examples.

In this very short Swift tutorial, you will learn how to loop or iterate over a dictionary in Swift and print out key/value pairs. Loop Through Dictionary // Create a Dictionary with two elements var myDictionary = [“first_name”: “Sergey”, “last_name”: “Kargopolov”] // Loop through dictionary and print key/value pair for (key,value) in myDictionary { print(“\(key)…

Read More Loop Through Dictionary in Swift

In this Swift tutorial, you will learn how to use the flatMap function in Swift which will flatten an Array of Arrays into a single array of elements. Given an Array of Arrays Let’s consider the following Array of Arrays. let numbers = [ [1,2,3,4], [5,6,7,8], [9,10,11,12]] The above array contains 3 arrays of integers. We…

Read More How to Use flatMap in Swift. Code Examples.

To practice Swift programming and learn how to create and position UI elements programmatically in Swift you can use Xcode to create a new project or you can use Xcode Playgrounds. In this short tutorial, you will learn how to use Xcode Playgrounds to run Swift code that creates a new UIView with a single…

Read More How to Use Xcode Playgrounds to Create UI in Swift