Chapter 5 : Swift Data types



In programming language we need to use different type of variables to store and transfer information from one view to another.

Whenever a variable is  created we reserve some space to the memory,So variable are the reserved memory locations to store values.

There are different type of variables in swift like character , string ,float , integer , boolean etc 

So according to our variable we declare the the operating system allocates memory according to it and store values in these reserved memory.

Swift have below Data types:

 Build-in data types and user-defined data types

Build-in data types

Int – >Stands for Integer value. Whole number are used as Int ,we can use Int32 or Int64 for 32 and 64 bits signed Integer

UInt – >Stands for Unsigned Integer value. Whole number are used as UInt ,we can use UInt32 or UInt64 for 32 and 64 bits unsigned Integer 

For e.g. : -33 , -20 , 50 etc

Float – > Stands for floating point numbers,Its is 32-bits representation for floating digits.

For e.g.  : 0.202, 3.14 ,1.007 etc

Double – > Stands for floating point number but at 64-bit representation. It is used for big floating values. 

For e.g. : 9999.99 , 1023.9899

Bool – > Stands for boolean value. A boolean value is 1 or 0 / True or False / true or false.

String – > Stands for meaningful set of characters.

For e.g. : “welcome to 9to5iOS website”

Character – > Stands for single character string literal 

For e.g.  : “S”

Optional : Stands for either hold a value or no value.

Tuples :Stands for group the multiple values in a single compound value.

All the variable have bound to memory with a typical range which contain every variable type lower and upper limit

Below you can see range of them

Int8      -> 1 byte (bit width)-> -127 to 127

Int32    -> 4 byte (bit width)-> -2147483648 to 2147483647

Int64    -> 8 byte (bit width)-> -9223372036854775808 to 9223372036854775807

UInt8    -> 1 byte (bit width)->  0 to 255

UInt32  -> 4 byte (bit width)->  0 to 4294967295

UInt64  -> 8 byte (bit width)->  0 to 18446744073709551615

Float     -> 4 byte (bit width)->  1.2E-38 to 3.4E+38 (~6 digits)

Double  -> 8 byte (bit width)->  2.3E-308 to 1.7E+308 (~15 digits)

 

Type Aliases

Using type Aliases we can create new name for an existing type .

For e.g. 

 typealias 9to5iOS = Int

It means that compiler have now instruction to read 9to5iOS as Int value

typealias 9to5iOS = Int

var Kilometer : 9to5iOS = 500

print (Kilometer) , its output becomes 100 so 9to5iOS treated here as Int

Type Safety

Type Safety means in swift 4 that if we have a code expects a String we cannot pass Int in it by mistakenly.

For safety purpose swift 4 have type-checks which shows error on compile time 

For e.g. 

var var_1 = 100

var_1 = “Test string”

print(var_1)

above code on compiling give error that

cannot assign value of type ’string’ to type ‘Int’

Type Inference

It interfere what types our variable have.Type inference enable the compiler to deduce the type of  a particular expression automatically when it compiles our code. If the type of our var is assign at initial value then swift will infer it.

So during variable declare we need

 Explicitly declare its type 

For e.g. 

 var temp = “Test A string” // declare a string variable with initial value

 var temp1 : String         // initialize a variable without giving initial values, so it need to explicit define as type String

 temp1 = “Test B string”    // we assign string value to our variable temp1, if we tried to assign a different value it will give error 



2 Comments

Add a Comment

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