Objective c Xcode认为由于if/else语句中的返回,控件可能会到达非void函数的末尾
这是一个简单的问题:为什么以下代码会导致“控件可能达到非无效函数的末尾”警告?在什么情况下,两个return语句中的一个不会被命中?将第二个Objective c Xcode认为由于if/else语句中的返回,控件可能会到达非void函数的末尾,objective-c,if-statement,return,Objective C,If Statement,Return,这是一个简单的问题:为什么以下代码会导致“控件可能达到非无效函数的末尾”警告?在什么情况下,两个return语句中的一个不会被命中?将第二个return语句放在else块之外是否更标准?这确实让警告沉默了,但我很好奇为什么它会存在 - (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath { NSString *lineToFit = [[self
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;
}
}
编译器采用这种方式,因为“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;
}
它将具有您想要的行为,并且没有警告。您确实存在导致无法返回的条件: 如果
shouldDisplayExtra
存在并且cellType==kExtraCellTypeNone
则没有定义返回
您应该将else块添加到条件中:
if (cellType != kExtraCellTypeNone) {
[cellsToContainExtra setValue: [NSNumber numberWithInt: cellType] forKey: lineIDString];
return lineSize.height + actorSize.height + 200;
} else {
// return something here
}
朋友,这很简单,
如果我们将函数声明为int类型,并在If、for..etc语句中返回一个值,那么它将显示此警告bcz如果上述任何条件不满足,那么它将返回什么,因此我们应该在结尾或else块中返回一个any值。返回nil;为我工作
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
}
我希望它能帮助某些人…Mm,我已经这么做了(如前所述),但我很好奇为什么编译器无法解决它不会卡住:)代码看起来很可疑:
kExtraCellType
看起来像常量,而不是类型[self cellIsGoingToContainExtraView]
看起来它返回的是BOOL,而不是kExtraCellType
。如果没有识别单元的参数,它将如何计算任何有意义的内容?你应该重温一下Cocoa编码风格惯例。看看我的答案,也许会有帮助
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;