2013-03-06 93 views
3

這是一個簡單的問題:爲什麼下面的代碼會導致「控制可能會達到非void函數結束」警告?在什麼情況下,兩個回報聲明中的一個不會被擊中?將else塊之外的第二個return聲明放在外面是更加標準的編碼習慣嗎?這確實使警告無效,但我很好奇它爲什麼存在。Xcode認爲控制可能會因if/else語句中的返回而達到非void函數的末尾

- (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; 
    } 
} 
+0

代碼看起來腥反正:'kExtraCellType'看起來像一個常量,不是一個類型。 '[self cellIsGoingToContainExtraView]'看起來像返回一個BOOL,而不是'kExtraCellType'。如果沒有識別細胞的參數,它如何計算有意義的東西?你應該回顧Cocoa編碼風格的約定。 – 2013-03-06 13:32:57

+0

看到我的答案也許它有幫助http://stackoverflow.com/a/39275095/1470374 – zest 2016-09-01 15:06:17

回答

12

你確實有導致不歸路條件:

如果shouldDisplayExtra存在,cellType == kExtraCellTypeNone那麼有沒有定義的返回......

你應該一個else塊添加到條件:

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

就是這樣。好奇心滿意,謝謝:) – Luke 2013-03-06 13:42:41

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; 
} 

它將擁有你想要的行爲,再加上沒有任何警告。

+1

嗯,我已經這樣做(如上所述),但我很好奇,爲什麼編譯器不能解決它,它wouldn不會卡住:) – Luke 2013-03-06 13:23:22

2

朋友其如此簡單, 如果我們聲明int類型的函數,並且在if,for..etc語句中我們返回一個值,那麼它會顯示這個警告bcz,如果上述任何一個條件n不滿足,那麼它會返回什麼,所以我們應該返回任何值在結尾或在其他塊。

1

返回零;爲我工作。

-(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; 

}

我希望它可以幫助別人......

相關問題