Here, we will try to convert a non-numeric string into an integer number using Int() function. Then we will print the appropriate message on the console screen.
The Int() function returns an optional integer, then we need to unwrap the integer number.
Program/Source Code:
The source code to demonstrate Int() function with an invalid value is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we imported a package Swift to use the print() function using the below statement,
Here, we created a string strCode with the initial value "ABCDEF”. And, strCode does not contain a valid numeric value. Then Int() function return "nil" value. After that, we printed the appropriate message on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer