Optionals problem


#1

Around page 85, I’ve put this code into the playground but I’m getting different behavior than what the book says.

var reading1: Float?
var reading2: Float?
var reading3: Float?

reading1 = 9.8
reading2 = 9.2
//reading3 = 9.7

if let r1 = reading1,
    let r2 = reading2,
    let r3 = reading3 {
    let avgReading = (r1 + r2 + r3) / 3
}
else {
    let errorString = "Instrument reported a reading that was nil."
}

This gives me a couple of errors and warnings. Is there something about the newer versions of Xcode/Swift that make this not work the way the book describes?


#2

What are the errors?

Warnings are just saying that you have created variables, but you are not using them.

Your are most likely experiencing the effects of a version mismatch between the artifacts.


#3

Well, it seems like some combination of restarting Xcode and sometimes retyping the examples made everything work the way they describe it in the book. After going further into the book I’m noticing that sometimes output begins to stop displaying entirely on the right side of the playground and restarting seems to correct it. Also I closed some other running apps and system processes thinking they might interfere somehow. I tried this after Xcode stopped responding entirely a couple of times. Thanks for the reply.