Imagine you have an app that asks the user to enter his/her age using the keyboard. When your app allows a user to input text, what is captured for you is given as a String. Howeve: 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 set its value using the In initializer that takes an instance of String as input. Pass in userInputAge as the argument for the initializer. 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 the value of userInputAge. Is there anything about the value printed that seems off? Print userAge again, but this time unwrap userAge using the force unwrap operator. Now use optional binding to unwrap userAge. If userAge has a value, print it to the console.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Optionals1 - How do I do this practice exercise using Swift code? 

 

Imagine you have an app that asks the user to enter his/her age using the keyboard. When your app allows a user to 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 set its value using the Int
initializer that takes an instance of String as input. Pass in userInputAge as the argument for the initializer. 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 the value of userInputAge. Is there anything about the value printed that seems off?
Print userAge again, but this time unwrap userAge using the force unwrap operator.
Now use optional binding to unwrap userAge. If userAge has a value, print it to the console.
Transcribed Image Text:Imagine you have an app that asks the user to enter his/her age using the keyboard. When your app allows a user to 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 set its value using the Int initializer that takes an instance of String as input. Pass in userInputAge as the argument for the initializer. 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 the value of userInputAge. Is there anything about the value printed that seems off? Print userAge again, but this time unwrap userAge using the force unwrap operator. Now use optional binding to unwrap userAge. If userAge has a value, print it to the console.
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY