![]() |
VOOZH | about |
Before we start let's see what is type conversion. Type Conversion is used to check the type of instances to find out whether it belongs to a particular class type or not using the type conversion method or approach. Basically, a Type Conversion is a method in which we can convert one instance data type to another instance data type by casting the variable or value within braces. For Example: var stringToFloatNum: Float = Float(67891)!
In Swift, one data type is converted into another data type by an XCode compiler at the compiler time. The type conversions can only be applied to compatible data types like integer, string, float, double,...etc. In this article, we will be learning about the type conversions in a Swift programming language with some same examples converting a string data type to an integer data type, integer data type to string data type, integer data type to float data type, float data type to integer data type, float data type to string data type and string data type to a float data type.
Syntax:
var <variable_name>: <data_type> = <conver_data_type>(<value>)
1. Example for Converting a Number to String Data Type
var intNumberToString: String = String(intNumberValue)
2. Example for Converting a String to Float Data Type
var stringToFloatNum: Float = Float(decimalstringValue)!
Note: In type conversion, the destination data type cannot be smaller than the source data type.
Here is a few examples where Data Types in Swift can be used for Type Conversions.
Convert Integer to String
This is a swift program where we are accepting a variable value as an Integer data type and converting that Integer variable value to a string data type and displaying the value and type using a print statement.
Output:
Integer Value = 789 of type Int String Value = 789 of type String
Convert String to Integer
This is a swift program where we accept a variable value as a string data type and convert that string variable value to an integer data type and display the value and type using a print statement.
Output:
String Value = 789 of type String Integer Value = 789 of type Int
Convert Integer to Float
This is a swift program where we are accepting a variable value as an integer data type and converting that integer variable value to a float data type and displaying the value and type using print statements.
Output:
Integer Value = 789 of type Int Float Value = 789.0 of type Float
Convert Float to Integer
This is a swift program where we are accepting a variable value as a float data type and converting that float variable value to an Integer data type and displaying the value and type using a print statement.
Output:
Float Value = 789.0089 of type Float Integer Value = 789 of type Int
Convert String to Float
This is a swift program where we are accepting a variable value as a string data type and converting that string variable value to a float data type and displaying the value and type using a print statement.
Output:
String Value = 789 of type String Float Value = 789.0 of type Float