0

I have a object called message.body of type Any

The object returns nil but if I do some test equals nil, it says that message.body ALWAYS been true.

enter image description here

enter image description here

How I solve this?

EDIT

Thank you for all answers

This is my code:

extension FirstViewController: WKScriptMessageHandler {
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.name == "jsHandler" {
            print( "Value: " )
            if message.body != nil {
                print( message.body )
            }
        }
    }
}

Really is a strong type this Any of message.body.

Thank you very much @vacawama, I really researched the subject but did not find anything the way I researched.

Donadon
  • 113
  • 11
  • 1
    In this case you are testing equality with `Optional.none` and since it seems `message.body` is not an optional `Any`, it will come as always `true`. That should teach you to use `Any` only in exceptional cirmcumstances. – Sulthan Mar 05 '19 at 20:20
  • Is `message.body` an object whose type you defined, or something from an external library? – John Montgomery Mar 05 '19 at 20:20
  • 1
    Also, please add the actual code to your question, not just images. – John Montgomery Mar 05 '19 at 20:21
  • 1
    The warning clearly says that `message.body` is non-optional, it cannot be `nil` but it can be `"nil"`. And `message.body` is most likely something more specific than `Any`. Don't fight the strong type system. – vadian Mar 05 '19 at 20:28
  • You should avoid using Any unless it’s some old legacy objective-c code. Better change it on enumeration of your needs. – Konstantin Mar 05 '19 at 20:30
  • Hey @JohnMontgomery, I edit with the code. Thank you man! – Donadon Mar 05 '19 at 23:53
  • Thank you very much @vacawama, I really researched the subject but did not find anything the way I researched. – Donadon Mar 05 '19 at 23:57
  • Hey @Sulthan, really is this! I will study more about exceptional cirmcuntances! Your tip and mark of vacawama help me about my problem! – Donadon Mar 05 '19 at 23:59

0 Answers0