Swift in iOS: Data Types


In any programming language we use data types to store and manipulate the data in variables based on the type of data like integers, strings, characters, bool, etc.

Whenever we create a variable in Swift programming language, automatically the required space is reserved in memory for that variable and the space allocation in memory is varied based on the type of variable like integer or float or double, etc.
Data Type      Descriptione.g
IntInt or UInt stand for Integer and used for numbers like 1, 2, 3, 4, 5. We can use Int32, Int64 to define 32 or 64-bit signed integer, whereas UInt32 or UInt64 to define 32 or 64-bit unsigned integer variables. 
10
FloatFloat stand for floating value and it is used to represent numbers with fraction value like 245.344 and etc. It is used to hold the numbers with larger or smaller decimal points like 3.14, and -455.3344.
3.14 
-455.3344
DoubleDouble stands for Double means if we want to allocate the large number with fraction value then we use Double as a float data-type. We can use it as a 64-bit floating-point number and Double is more precise than Float. For example, 345.344544, and -4554.3455543.
345.344544 
-4554.3455543 
BoolBool represent only two values either TRUE or FALSE. We can use Bool to check whether certain condition met or not.
let i = 10
if i> 20 {
// your code
}
StringString is a combination of Characters. We can define string data type by adding double quotes to our text like “Hello World”.
""
"welcome to tutlane"
CharacterIt represent a single alphabet character like “H”, “e”, “L”, “L”, “o”.“H”
OptionalIt represent a variable can hold value or no value.

Swift Data Type Ranges

Following table shows the details like how much memory space occupied by a variable based on the data type and minimum & maximum values range for each data type.

Data TypeMemory AllocationMin & Max Range
Int81byte-127 to 127
UInt81byte0 to 255
Int324bytes-2147483648 to 2147483647
UInt324bytes0 to 4294967295
Int648bytes-9223372036854775808 to 9223372036854775807
UInt648bytes0 to 18446744073709551615
Float4bytes1.2E-38 to 3.4E+38 (~6 digits)
Double8bytes2.3E-308 to 1.7E+308 (~15 digits)


Type Aliases

In swift by using typealiases keyword we can create new name for existing type. The typealiases are useful whenever we want to create meaningful name for our existing types.

Following is the syntax of creating alternative name to existing type using the keyword typealias.

typealias <NameOfDataType> = <Data_Type>

Following is the simple example of using typealias to create alternative name for “String” type.

typealias PersonEmail = String
var email : PersonEmail"rash@gmail.com"
print(email)
If you observe above example we created alternative name “PersonName” to “String” data type.

When we compile the above the code, compiler gives the following output

rash@gmail.com

Type Safety

Swift is a type-safe programming language that means once we define variable with one data type then it’s not possible to assign another data type value to it.

Suppose if we define variable with String data type and if we try to pass an integer value then the compiler will throw error like as shown following because swift is a type-safe language and it perform type checks while compiling our code and flags any mismatched types as errors.


Type Inference

In swift Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.

Following is the simple example to deduce the variable value, when the compiler found that it adds the floating values to integer.

var numberOfApples = 20
print(numberOfApples)
var name = "Rashpinder"
print(name)
var marks = 80.5
print(marks)
When we run above program we will get output like as shown below.

20
Rashpinder
80.5



Things to remember

1. Since swift is a type inference language, variables or constants can automatically infer the type from the value stored. So, you can skip the type while creating variable or constant. However you may consider writing the type for readability purpose but it’s not recommended.
Type inferred variable/constant
Swift compiler can automatically infer the variable to be of String type because of its value.
2. Swift is a type safe language. If you define a variable to be of certain type you cannot change later it with another data type.
 Swift is a type safe language
The above code will create an error because we already specified that variable playerName is going to be a String. So we cannot store a Integer number in it.

Comments

Popular posts from this blog