Swift Type Casting

Type casting is a way to modified one data type values to another data type. That is very useful mechanism because that is change the behaviour of values (variables value or literal) to another form. We are know about that when we declare a variable in our program then data type are can not modified. Type casting is provide an availability to modified one data type value to another data type value in safely. And value properties are change to relative convert data type. For example.

let intValue = 10 

let stringValue = String(intValue) //Convert integer variable value to string

print("Add : \(intValue+50)") //integer operation, add two integers
print("Add : \(stringValue+"50")") //String operation, add two string

Observe that in this example perform addition of two strings and integers. Addition of two integer number is produce an integer result And addition of two string is produce string result. Some cases we are need to modified one data type values to another data type, Which is work on another data type properties. In above example is produce this result.

Add : 60
Add : 1050

How to casting one data type values (variable values) to another data type?. That is very simple , If you are know about that in Swift Programing every data types (Int, String, Float, Double) is an class.

When we are create a variable that is internally use of those class properties. So we can use this class constructo to modified the data type of value. This Syntax as follows.

ClassName(value) //Modified data class name

or

DataType(value) //Modified data type

Example
String(value/variable_value) //Convert value to string

Int(value/variable_value) //Convert value to Integer

Double(value/variable_value) //Convert value to Double

Note that variable data type are not modified. Their value will be modified. And modified value can possible to use perform relative data type operation. And also possible this value can be assigned to similar data type variable. Let an example.

let pinCode : Int =  101010

let address : String = """
House no 007 Sector programing, 
nearby Swift Compiler,
Pin code : 
""" + String(pinCode) //Number to String Conversion

print("Address : \(address)")

Output

Address : House no 007 Sector programing, 
nearby Swift Compiler,
Pin code : 101010

Note that we cannot added two different data type value directly in swift programming. In this example are using the variable value we can also used constant literal.

Restriction and limitation of type casting

There are some limitation of swift data type. For example integer variable can hold integer value but not store fraction decimal value. String is capable to hold characters values. Double can stored fraction decimal value.

So we cannot convert all words to an integers. Only which string word are convert which are contain numeric character. See this example.

let myInts : Int = Int("123")! //Valid numeric string

let otherInts : Int = Int("ABC")! //not valid
print(myInt)

In this program (!) are used to unwrapped optional value to integer variable. Because compiler are provide an compilation error (Swift version 4). In this program are not execute successfully because we are not giving valid numeric string to Int class constructor. That is produce error like this way.

swift test.swift
Fatal error: Unexpectedly found nil while unwrapping an Optional value
Current stack trace:
:
:
:
Stack dump:
0.  Program arguments: /usr/share/swift/usr/bin/swift -frontend -interpret test.swift -disable-objc-interop -color-diagnostics -module-name test 
Illegal instruction (core dumped)

Avoid this type of situation by using provide a valid format to convert specified data type. When we are remove second statement of above program then this working. see this example.

let myInts : Int = Int("123")! //Valid numeric string

//let otherInts : Int = Int("ABC")! //not valid
print(myInts)//123

One other important point when we are convert decimal value to integer that are not get fraction decimal values. See this example.

let myInts : Int = Int(123.5000)

print(myInts) //123

Decimal value is an type of number. So in this situation cannot using (!) unwrapped optional. And resultant of this program is form of whole number.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved