Problem in print - chapter 3 page 73

i keep getting the same error that i can’t resolve, can anyone help me?

in this function:
func print<T: CustomStringConvertible>(label: String, event: Event) {
print(label, event.element ?? event.error ?? event)
}

i get this error:
error: RxSwiftPlayground.playground:49:32: error: value of optional type ‘Any?’ not unwrapped; did you mean to use ‘!’ or ‘?’?
print(label, event.element ?? event.error ?? event)

@scotteg Can you please help with this when you get a chance? Thank you - much appreciated! :]

@rcrdcst Thanks very much for your question!

One of the ways I debug code when I’m going through a book is by comparing what I have with the books source code. Have you tried downloading the source code and running it separately? I’m asking this because if it’s a problem with the source code, then others will face the same issue, and we’ll have to look into making a correction. If not, then it is possible that perhaps the error could be something else.

Try that if you haven’t already and get back to us! If you have, then I apologize.

Hi @rcrdcst, I just confirmed that I do not get this warning in the book source code. There are 7 calls to that print(label:event:) function in the playground. You could try commenting them all out and uncommenting one at a time to see which call(s) cause the warning, and then compare that code with the book source code final playground. Let us know how that goes. Thanks!

I get the same error. It is an error, not a warning. Maybe it is because I am running Xcode 10?

Not sure, I got around it for now by simply changing it to:

print(label, (event.element ?? event.error ?? event) ?? event)

Hope this helps!

For the recode… if you want running in normal Xcode10 (not in playground)… You should like this,

func print<T: CustomStringConvertible>(label: String, event: Event) {
Swift.print(label, (event.element ?? event.error) ?? event)
}

This topic was automatically closed after 166 days. New replies are no longer allowed.