Data Types In Swift

The most crucial component of any programming language is its data types. Similar to other programming languages, Swift uses variables to store data, and the data types of the variables determine what kind of data can be stored there. Since variables are only reserved memory to hold data, the operating system allows memory based on these data types and only stores the appropriate data type there. For instance, an integer-type variable can only hold integer values; string values cannot be stored. The six data types that Swift supports are Int, String, Float, Double, Bool, and Character. Let’s go over these data kinds in more detail using some examples.

For example:

Consider a scenario in which you need to store company information, including the name and phone number of the company. You must create two variables or constants to store the data because the phone number is a number (7075764665) and the name is a string (Codegeekworld).

You can accomplish this in Swift 4 by declaring variables and the data type as follows:

var comapnyName: String = "codegeekworld"
var phoneNumber: Int = 7075764665

In this case, we defined the variables phone Number (of type Int, which stores 7075764665) and company name (of type String, which stores “codegeekworld”).

If the following syntax is used, a compile-time error will occur:

It indicates that only the Int data type, not the String data type, can store numbers.

Built-in Data Types

A wide variety of user-defined and built-in data types are available to programmers in Swift 4. Typically, when variables are declared, the following basic data types are used −

The most popular data types in Swift 4 are:

Integer Data type

  • Positive and negative numbers, including zero, can be stored in the integer data type without the use of fractional values.
  • Range: -9223372036854775808 to 9223372036854775807 (64-bit platform) -2,147,483,648 to 2,147,483,647 (32-bit platform)
  • If you want to specify the storage type, there are numerous other variants of the integer data type that you can use, including UInt, Int8, Int16, and so on.
  • The platform type determines its size, which can range from 32 bits to 64 bits.
  • It has a default value of 0.

Integer Data type example:

Output:

80
50

The program above generates an output of Codegeekworld by first declaring a String variable called companyName and then assigning a value of Codegeekworld to it.

The output is Blogging Company because we later changed the value to Blogging Company by using the assignment operator companyName = Blogging Company.

The Int data type has additional variations in Swift Like Int8(Its default value is 0, size is 8 bit and range varies from -128 to 127), UInt(Its default value is 0, 32/64 bit depending on the platform type), .min Int8(-128) & .max Int8(127).

Float Data type

Fractional and decimal numbers can be stored in float data types. With a size of 32 bits and a range of 1.2 * 10–38 to 3.4 * 1038 (~6 digits), its default value is 0.0.

Output:

89.5

Double Data type

Although Float supports larger decimal points than Double does, Double can store fractional or decimal numbers.

Its range is 2.310-308 to 1.710308 (~15 digits), with 0.0 as its default value and 64-bit size.

Output:

2.1416

String Data type

A collection of characters can be stored in a string data type. It is of the Value type, and “” (Empty String) is its default value.

String data type example:

Output:

Codegeekworld
Blogging Company

Character Data type

A single-character string is represented by the character data type. Swift’s character type is a more up-to-date, quicker character implementation. The Character keyword in Swift is used to define variables of the character type. Take a look at the syntax below, for example.

Character data type example:

Output:

a
b
z
?

Booleans Data type

There are only two values that the Bool data type can store: true and false.
False is its default value.
Usually, it is utilized in if-else statements. In conditional statements, they are frequently used.

Booleans data type example:

Output:

Bloging true

Conclusion

It is essential to comprehend Swift’s fundamental data types in order to write clear, effective code. Gaining proficiency in working with strings, booleans, floating-point numbers, and integers will put you well on your path to becoming a skilled Swift developer. Keep checking back for additional Swift tutorials to advance your understanding and proficiency.

The Swift Data types are Complete!

I’m grateful. I hope this is useful to you. Kindly inform me if you have any questions.

Leave a Reply

Your email address will not be published. Required fields are marked *