Morgan Wilde
Morgan Wilde

Reputation: 17295

Switch statement must be exhaustive

I have the following switch statement and it seems to be perfectly exhaustive.

switch (point.x > frame.origin.x, point.y > frame.origin.y) {
case (true, true): // Bottom right
    moveSubviewX = subview.frame.origin.x - moveBy
    moveSubviewY = subview.frame.origin.y - moveBy
case (false, true): // Bottom left
    moveSubviewX = subview.frame.origin.x + moveBy
    moveSubviewY = subview.frame.origin.y - moveBy
case (true, false): // Top right
    moveSubviewX = subview.frame.origin.x - moveBy
    moveSubviewY = subview.frame.origin.y + moveBy
case (false, false): // Top left
    moveSubviewX = subview.frame.origin.x + moveBy
    moveSubviewY = subview.frame.origin.y + moveBy
}

With a tuple that has two Bool components there are only 4 possible variations. Why do I still have a suggestion to include "a default clause"?

Upvotes: 0

Views: 2041

Answers (1)

Adis
Adis

Reputation: 4552

Xcode checks if the switch statement is exhaustive only if you're switching enums. For every other case, it checks if there is a default statement, and if not, it puts up a warning.

You can either use enums, or squelch the warning if you want to, or, just add the missing default statement doing nothing.

Upvotes: 7

Related Questions