Published by Arunprasadh C on 21 Apr 2022Last Updated on 28 Apr 2022

Data Types available in Swift

In any programming language, Data Types are quite important as they decide the amount of memory to be allocated to a variable/constant. Swift supports the following Built-in Data Types :

Now that we have seen the various Data types, let’s see how much space is allocated for variables of each Data Type and the range of values that can be represented using each data type:

Data Type Space required for a single Variable Range of values that can be represented
Int8 1 byte -128 to 127
UInt8 1 byte 0 to 255
Int16 2 bytes -32768 to 32767
UInt16 2 bytes 0 to 65535
Int32 4 bytes -2147483648 to 2147483647
UInt32 4 bytes 0 to 4294967295
Int64 8 bytes -9223372036854775808 to 9223372036854775807
UInt64 8 bytes 0 to 18446744073709551615
Float 4 bytes 1.2E-38 to 3.4E+38 (~6 digits)
Double 8 bytes 2.3E-308 to 1.7E+308 (~15 digits)
Bool 1 byte Range not Applicable. Can hold either true or false

Type Aliases

Swift allows us to define new names for existing Data Types. These new names can be used as actual Data Type Names. Type Aliases can be defined using the following Syntax:

typealias newName = Type

Example :

typealias Number = Int
var x: Number = 56

Type Safety

Swift is a type-safe language, which means that we can’t assign a literal or object of a different type to a variable/constant of some other type. For example, the following code will produce the error error: cannot assign value of type 'String' to type 'Int' at Compile time :

var a: Int = 5
a = "Hello"

Type Inference

Swift can infer the Data Type from the type of literal/object assigned to a variable/constant.

Example :

var a = 4 // Inferred as Int
var b = "Hello World" // Inferred as String

Now that we have seen basically about Data types in Swift, we can move on to see in Detail about Characters and Strings in Swift.

← Back to Index
← Introduction to Swift Characters and Strings →