Question: Exercise - Optional Note: chosen Language Swift Imagine you have an app that asks the user to enter his/her age using the keyboard. When your
Exercise - Optional
Note: chosen Language "Swift"
Imagine you have an app that asks the user to enter his/her age using the keyboard. When your app allows a user the input text, what is captured for you is given as a String. However, you want to store this information as an Int. Is it possible for the user to make a mistake and for the input to not match the type you want to store?
Declare a constant userInputAge of type String and assign it "34e" to simulate a typo while typing age. Then declare a constant userAge of type Int and use the Int initializer and pass in userInputAge. What error do you get?
Go back and change the type of userAge to Int?, and print the value of userAge. Why is userAge's value nil? Provide your answer in a comment or print statement below.
Now go back and fix the typo on userInputAge. Is there anything about the value printed that seems off?
Print userAge again, but this time unwrap userAge using the bang operator.
Now use optional binding to unwrap userAge. If userAge has a value, print it to the console.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
