Swift Numbers

Kiarash Vosough
5 min readMar 26, 2022

--

Move Your Knowledge About Numbers To Next Step

Introduction

In every programming language, there are several primitive types available, including numbers, string, char, etc.
Numbers play an important role in programming and even computer science. As we know, computers are built from bits, and every sequence of bit represent a specific number. So it is essential to learn them.

Readers of this article are assumed to be aware of the protocol, Generic, and other swift language basics.

What is going to be covered?

In this article series, you will learn about Swift Number types, such as Int, UInt, Double, Float, and specifically the protocols they conform to.

Some Basis Protocol

There are some basic protocols that almost every type of swift conforms to them, and you must know what they are used for, as they become handy most of the time. Before I discuss the Numbers, these protocols are covered.

Equatable

The straightforward one is Equatable. This protocol support overloading equality and non-equality (with default implementation) symbol for your types. It returns a boolean indicating whether the left and right values are equal. So until here, there is a way to recognize whether two numbers are equal.

Swift Compiler can synthesize the Equatable protocol inside the struct until all their properties are Equatable.

Comparable

The second one is Comparable. It brings the comparing symbols to the scene. As the result of conformance to this protocol, you can compare the same two types with several symbols. The first method, which overload < is mandatory. The rest has a default implementation. You can find more operator overloaded if you look inside extensions of comparable. This protocol will compare two types which is what we as humans do automatically with our minds. Furthermore, you learned it in elementary school.

Notice that Comparable also conforms to Equatable.

Strideable

Here it comes more complicated than the previous, Strideable. As the swift definition says:

A type representing continuous, one-dimensional values that can be offset and measured.

So let us see what exactly this means.

First, you should pay attention that it conforms to Equatable.

Second, an associated type should conform to Comparable and SignedNumeric.

SignedNumeric will soon be covered, but you may know the signed(from -inf to +inf) and unsigned(from 0 to +inf) numbers, and Stride should be the signed one because we want them to be offset forward and backward.

This generic type Stride represents the type of value indicating the distance between Strideables, ex, the distance between 1 and 20 as an integer value is type integer 19.

Notice that the counting step depends on the implementation of these methods for each type, ex Int plus the given offset n with its current value. However, you can multiply the offset with the current value on your implementation.

The capability of summing numbers of type Stride is inherited from AdditiveNumerics, which will be discussed later. So do not be surprised why the Stride type inside the protocol can be summed.

This protocol does offset and measures the offset between two values. It is somehow counting with the given step ex. We want to count from zero to 20 with 2 steps. We start advancing 0 by 2 and so on until we reach 20 forward and backward, we can begin advancing 20 by -2, and the distance between 0 and 20 can be measured.

Another Example is Dates. We can walk through the dates and measure the target date we want because the Date conforms to Stridable and the Stride type is TimeInterval. Also, it is possible to calculate the TimeInterval between two Dates.

So what is the common usage of Strideables?

It becomes handy when you need to create a sequence of values that every x was the combination of the x and the n that was passed to the method.

Let us see two examples below:

The stride function is part of the swift Standart Library and creates a sequence with the given start, end, and the step of striding. Here the purpose was to count radians from zero to 2*pi(360 degrees) with the step of pi/2(90 degrees).

Notice that the end of the range is open, so it will not be considered.

BloodGroup is Strideable, and the Stride is of type int8. That is because the ASCII values are used to evaluate the following word(explaining the conversion is beyond the scope of this article).

The Stride method was used to create a sequence of BloodGroup by advancing each generated BloodGroup by 1( 1 was added to the ASCII value of the current word to get the next word).

You might again ask why we did not use UInt8 for Stride type because UInt8 does not conform to SignedNumeric; instead, it conforms to UnsignedInteger.

I do not want to blow your mind, so we postpone these types of discussions to the following articles.

Till now, you observed this hierarchy. So try to dig a little more into these protocols, and do not miss the AppleDocs for more information.

We will discuss the Numeric and AdditiveNumeric in the following article.

I hope that was helpful for you. Have a good Swifty day:)

--

--

Kiarash Vosough

iOS Developer and Swift Lover with over 5 years of experience on Apple platforms. Find me on Any Social Media by searching Kiarash Vosough:)