Many APIS that give you information gathered by the hardware return optionals. For example, an API for working with a heart rate monitor may give you nil if the heart rate monitor is adjusted poorly and cannot properly read the user's heart rate. Declare a variable heartRate of type Int? and set it to nil. Print the value. 10 11 In this example, if the user fixes the positioning of the heart rate monitor, the app may get a proper heart rate reading. Below, update the value of heartRate to 74. Print the value. 13 14 As you've done in other app exercises, create a variable hrAverage of type Int and use the values stored below and the value of heartRate to calculate an average heart rate. 16 let oldHR1 = 80 17 let oldHR2 = 76 18 let oldHR3 = 79 19 let oldHR4 - 70 20 21 If you didn't unwrap the value of heartRate, you've probably noticed that you cannot perform mathematical operations on an optional value. You will first need to unwrap heartRate. Safely unwrap the value of heartRate using optional binding. If it has a value, calculate the average heart rate using that value and the older heart rates stored above. If it doesn't have a value, calculate the average heart rate using only the older heart rates. In each case, print the value of hrAverage.
Many APIS that give you information gathered by the hardware return optionals. For example, an API for working with a heart rate monitor may give you nil if the heart rate monitor is adjusted poorly and cannot properly read the user's heart rate. Declare a variable heartRate of type Int? and set it to nil. Print the value. 10 11 In this example, if the user fixes the positioning of the heart rate monitor, the app may get a proper heart rate reading. Below, update the value of heartRate to 74. Print the value. 13 14 As you've done in other app exercises, create a variable hrAverage of type Int and use the values stored below and the value of heartRate to calculate an average heart rate. 16 let oldHR1 = 80 17 let oldHR2 = 76 18 let oldHR3 = 79 19 let oldHR4 - 70 20 21 If you didn't unwrap the value of heartRate, you've probably noticed that you cannot perform mathematical operations on an optional value. You will first need to unwrap heartRate. Safely unwrap the value of heartRate using optional binding. If it has a value, calculate the average heart rate using that value and the older heart rates stored above. If it doesn't have a value, calculate the average heart rate using only the older heart rates. In each case, print the value of hrAverage.
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...
Related questions
Question
Optionals 2/6 - How do I do this practice exercise using Swift code?
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
Recommended textbooks for you
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 Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
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 Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
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
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY