swift - Binary operator '~=' cannot be applied to operands of type 'String' and 'String?' -


i have simple switch-statement not simple.

switch(bubble?.name){ //bubble skphysicsbody     case "largebubble": // <= error         newbubblesize = "medium"         break;     default:         newbubblesize = "large"         break; } 

here error mentioned in title binary operator '~=' cannot applied operands of type 'string' , 'string?'. , have no clue why problem 1 of them optional.

because of optional chaining, bubble?.name has type string?. have few options:

  • use "largebubble"? in case expression (swift 2+ only).
  • check nil before doing switch, switch argument string instead of string?.
  • use bubble!.name (or bubble.name if it's skphysicsbody!) if absolutely sure won't nil

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -