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"?incaseexpression (swift 2+ only). - check nil before doing
switch, switch argumentstringinstead ofstring?. - use
bubble!.name(orbubble.nameif it'sskphysicsbody!) if absolutely sure won't nil
Comments
Post a Comment