4

これは簡単な質問です。次のコードで「コントロールが非void関数の終わりに到達する可能性があります」という警告が表示されるのはなぜですか。どのような状況で、2つのreturnステートメントのいずれかがヒットしませんか?return2番目のステートメントを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;
    }
}
4

4 に答える 4

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
    }
于 2013-03-06T13:25:42.013 に答える
2

友達はとても単純です。int型の関数を宣言し、if、for..etcステートメントに値を返すと、上記の条件のいずれかが満たされない場合にこの警告bczが表示され、何が返されるのでしょうか。末尾またはelseブロックで任意の値を返します。

于 2014-01-09T18:50:55.103 に答える
1

nilを返します。私のために働いた。

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

{{

if(tableViewObj == tableView) //here u can make decision
{

UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y, 320, 40)];

    UIButton *addcharity=[UIButton buttonWithType:UIButtonTypeCustom];

    [addcharity setTitle:@"Help" forState:UIControlStateNormal];
    [addcharity addTarget:self action:@selector(addCharity:) forControlEvents:UIControlEventTouchUpInside];

    [addcharity setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];


    addcharity.frame=CGRectMake(0, 0, 130, 30); //set some large width to ur title

    [footerView addSubview:addcharity];

    return footerView;
}

return nil;

}

私はそれが誰かを助けることを願っています...

于 2015-05-01T16:46:50.167 に答える
0

「main」ブロックにはreturnステートメントがないため、コンパイラーはそのように処理します。

代わりにこれを実行してみてください。そうすれば、警告は消えます。

- (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;
        }
    }
        // Return the line height, actor height, plus a buffer.
        return lineSize.height + actorSize.height;
}

意図した動作に加えて、警告はありません。

于 2013-03-06T13:22:38.660 に答える