I need help solving the code problem

I need help solving the question below. Ive completed some part of it
Declare a variable heartRate of type Int? and set it to nil. Print the value.
var heartRate : Int? = nil

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.
heartRate = 74

print(heartRate)

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.
var hrAverage: Int!

let oldHR1 = 80

let oldHR2 = 76

let oldHR3 = 79

let oldHR4 = 70

hrAverage = (oldHR1 + oldHR2 + oldHR3 + oldHR4 + heartRate!)/5

print(hrAverage)

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.

this problem wants you to use optional binding,, this is how you use optional binding:

// `T?` is shorthand for `Optional<T>`
let foo:Optional<T> = // ...
// we are shadowing the variable name `foo` within the `if` block. 
// shadowing means a variable inside a scope has the same name as 
// a variable outside that scope, but refers to a different value.
if let foo:T = foo 
{
    // do something with `foo`, which is of type `T` within the 
    // scope of this `if` block.
}
else 
{
    // handle the case where `foo` is `nil`. note that the `foo` 
    // in this `else` block is the original `foo` of type `T?`. 
    // the `let foo:T = foo` binding earlier only applies to the 
    // `if` branch of the `if`-`else` statement.
}

you can also use a guard statement, if the if branch is the expected execution path, and the else branch is the exceptional path.

let foo:Optional<T> = // ...
// `guard` statements do not create a new scope for the affirmative 
// branch (the `if` branch in the last example), so we are not allowed 
// to shadow the variable name `foo`. (Unless `foo` is a function 
// parameter name.)
guard let bar:T = foo 
else 
{
    // handle the case where `foo` is `nil`
}

// do something with `bar`

do you have enough to solve the problem now?


btw, i don’t know why hrAverage is declared as an implicitly unwrapped optional (Int!), but there’s no need to, and you should (almost) never use them anyway.

Thank you so much Taylor. Do you know a website that I can practice iOS development project on ?

none that i know and would recommend, sorry. Godbolt is good for understanding how swift code translates into linear imperative code though, and if you haven’t learned how to use it already, the swift REPL is very very useful for mucking around to learn the semantics of the language that aren’t in any of the written guides.

Terms of Service

Privacy Policy

Cookie Policy