控制可能会到达非空函数错误if-statement [英] Control may reach end of non-void function error if-statement

查看:770
本文介绍了控制可能会到达非空函数错误if-statement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误控件可能会在此代码上达到非空函数的结尾:

I'm getting the error Control may reach end of non-void function on this code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (changeData.selectedSegmentIndex == 0) {
        return self.tweets.count;
    } else if (changeData.selectedSegmentIndex == 1) {
        return self.tweets1.count;
    } else if (changeData.selectedSegmentIndex == 2) {
        return self.tweets2.count;
    }
}

为什么?

推荐答案

因为当你的所有 if 条件失败时,你不会从函数中返回任何内容。

Because when your all if condition fails, you are not returning anything from the function.

函数中的多个return语句也不是一个好习惯。

Also multiple return statement in a function is not a good practice.

这样做:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int count = 0;
    if (changeData.selectedSegmentIndex == 0)
    {
        count = self.tweets.count;
    }
    elset if (changeData.selectedSegmentIndex == 1)
    {
        count  = self.tweets1.count;
    }
    else if (changeData.selectedSegmentIndex == 2)
    {
        count  = self.tweets2.count;
    }
    return count;
}

这篇关于控制可能会到达非空函数错误if-statement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆