Xcode认为由于if/else语句中的返回,控制可能会到达非void函数的末尾

Luk*_*uke 4 if-statement return objective-c

这是一个简单的问题:为什么以下代码会导致"控件可能达到非空函数结束"警告?在什么情况下,两个返回语句中的一个不会被击中?将第二个return语句置于else块外是否是更标准的编码实践?这确实使警告无声,但我很好奇为什么它存在.

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*own 13

你确实有条件导致没有回报:

如果shouldDisplayExtra存在cellType == kExtraCellTypeNone然后没有定义的返回...

您应该在条件中添加else块:

    if (cellType != kExtraCellTypeNone) {
        [cellsToContainExtra setValue: [NSNumber numberWithInt: cellType] forKey: lineIDString];
        return lineSize.height + actorSize.height + 200;
    } else {
       // return something here
    }
Run Code Online (Sandbox Code Playgroud)