Swift Data Types Swift Programming

What is Swift Data Types?

While performing programming in any programming language, you require to employvarious types of variables for storingdata. Variables are reserved memory locations for storing values. This means that when you create a variable, you willallocate some space in the memory.

You may want to store data of different data types such as string, character, integer, floating point, wide character, Boolean, etc. depending on the data type of a variable, the OS willassigns memory and determines what could be stored in the reserved memory.

What are Built-in Data Types?

Swift provides the programmer a rich assortment of built-in and also user-defined data types. The below types of basic data types are most common when declaring variables −

  • Int or Uint − This is employed for whole numbers. More specifically, you mayemploy Int32, Int64 for defining 32 or 64 bit signed integer, on the other hand UInt32 or UInt64 for defining 32 or 64 bit unsigned integer variables. For instance, 42 and -23.
  • Float - This is employedfor representing a 32-bit floating-point number as well as numbers with smaller decimal points. For instance, 3.14159, 0.1, and -273.158.
  • Double − This is employedfor representing a 64-bit floating-point number and employed when floating-point values should be very large. For instance, 3.14159, 0.1, and -273.158.
  • Bool − This represents a Boolean value that may beeither true or false.
  • String − This is an ordered collection of characters. For instance, "Hello, World!"
  • Character− This is a single-character string literal. For instance, "C"
  • Optional− This represents a variable that mayaccept either a value or no value.

We have listed here somemajor points associated to Integer types −

  • On a 32-bit platform, Int is the similar size as Int32.
  • On a 64-bit platform, Int is the similar size as Int64.
  • On a 32-bit platform, UInt is the similar size as UInt32.
  • On a 64-bit platform, UInt is the similar size as UInt64.

Int8, Int16, Int32, Int64 can be employedfor representing 8 Bit, 16 Bit, 32 Bit, and 64 Bit forms of signed integer.

UInt8, UInt16, UInt32, and UInt64 can be employedfor representing 8 Bit, 16 Bit, 32 Bit and 64 Bit forms of unsigned integer.

What are Bound Values?

The below table shows the variable type, how much memory it holdsfor storing the value in memory, and what is the maximum and minimum value that can be stored in such type of variables.

Type

Typical Bit Width

Typical Range

Int8

1byte

-127 to 127

UInt8

1byte

0 to 255

Int32

4bytes

-2147483648 to 2147483647

UInt32

4bytes

0 to 4294967295

Int64

8bytes

-9223372036854775808 to 9223372036854775807

UInt64

8bytes

0 to 18446744073709551615

Float

4bytes

1.2E-38 to 3.4E+38 (~6 digits)

Double

8bytes

2.3E-308 to 1.7E+308 (~15 digits)

What are Type Aliases?

You can create a new name for an prevaling type withtypealias. Below is a simple syntax for defining a new type with typealias −

For instance, the below line instructs the compiler that Feet is other name for Int −

Now, the below declaration is perfectly legal and creates an integer variable known as distance −

If we run the above program with playground, we will obtain the below output −

What are Type Safety?

Swift is a type-safe language i.e if a part of your code is expecting a String, you are not supposed to pass it an Int.

As Swift is type-safe, it will perform type-checks during compilation of your code and flags any mismatched types as errors.

After compiling the above program, it will give the below compile time error.

What are Type Inference?

Type inference will enable a compiler to conclude the type of a specific expression accordingly when it compiles your code, just by examining the values you facilitate. Swift employs type inference to work out the suitable type as shown below.

After running the above program with playground, we will obtain the below output −

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Swift Programming Topics