Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode thinks control may reach end of non-void function due to returns in if/else statements

This is a simple question: why does the following code cause a "Control may reach end of-non-void function" warning? Under what circumstances would one of the two return statements not be hit? Would it be more standard coding practise to place the second return statement outside of the else block? This does silence the warning, but I'm curious as to why it exists at all.

- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
    NSString *lineToFit = [[self.fetchedResultsController objectAtIndexPath: indexPath] line];
    NSString *actorToFit = [[self.fetchedResultsController objectAtIndexPath: indexPath] actor];
    CGSize lineSize = [lineToFit sizeWithFont: [UIFont boldSystemFontOfSize: 12.0f] constrainedToSize: CGSizeMake(320, 800)];
    CGSize actorSize = [actorToFit sizeWithFont: [UIFont boldSystemFontOfSize: 12.0f] constrainedToSize: CGSizeMake(320, 800)];

    if (shouldDisplayExtra) {
        kExtraCellType cellType = [self cellIsGoingToContainExtraView];
        if (cellType != kExtraCellTypeNone) {
            [cellsToContainExtra setValue: [NSNumber numberWithInt: cellType] forKey: lineIDString];
            return lineSize.height + actorSize.height + 200;
        }
    } else {
        // Return the line height, actor height, plus a buffer.
        return lineSize.height + actorSize.height;
    }
}
like image 200
Luke Avatar asked Dec 04 '22 12:12

Luke


1 Answers

You do have a condition that results in no return:

if shouldDisplayExtra exist and cellType == kExtraCellTypeNone then there is no return defined...

You should add an else block to the conditional:

    if (cellType != kExtraCellTypeNone) {
        [cellsToContainExtra setValue: [NSNumber numberWithInt: cellType] forKey: lineIDString];
        return lineSize.height + actorSize.height + 200;
    } else {
       // return something here
    }
like image 198
Richard Brown Avatar answered May 21 '23 07:05

Richard Brown