checking for nil and getting ‘unexpectedly found nil while unwrapping an Optional value’

I’m checking if an optional value is nil before unwrapping it

        //check if it's nil
        if voteStatus == nil {
            cell.hideButtons();
        } else {
            cell.setStatus(voteStatus!); //crash here, why?
            if voteStatus == 1 {
                cell.showButtons();
            } else {
                cell.hideButtons();
            }
        }

However, it crashes at this line:

                cell.setStatus(voteStatus!);

with error:

unexpectedly found nil while unwrapping an Optional value

Doesn’t make any sense to me because I’m checking if it’s nil above before unwrapping it.

What am I doing wrong?


Source: ios

Leave a Reply