1

Please consider following code:

-(NSString*)getNextOvulationString{

    if (self.daysToNextOvulation < 0){
       return [NSString stringWithFormat:@"Овуляция была %li дней назад", labs(self.daysToNextOvulation)];
    }   else if (self.daysToNextOvulation == 0){
        return [NSString stringWithFormat:@"Овуляция началась"];
    }   else if (self.daysToNextOvulation   > 0){
        return [NSString stringWithFormat:@"Овуляция началась"];
    }
 }

There is 3 cases, and obvious, we will get one guaranteed.

Still, compiler not allow me to build, because of error:

Control reach end of non-void function

How to fix that?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • 5
    Don't put the "if" test for the last one. Do `} else{// if (self.daysToNextOvulation > 0){` instead, that way you keep for coming back later that it's the case for "> 0". The compiler doesn't check if all the values possibles are checked. – Larme Aug 26 '16 at 16:02
  • 1
    you know that but compiler doesn't know it. We should always leave one return statement outside of if block or in else as Larme suggested. – Teja Nandamuri Aug 26 '16 at 16:05

2 Answers2

2

You need to add else at last to return the NSString, so change you if condition like this.

-(NSString*)getNextOvulationString{

    if (self.daysToNextOvulation < 0){
        return [NSString stringWithFormat:@"Овуляция была %li дней назад", labs(self.daysToNextOvulation)];
    }
    else if (self.daysToNextOvulation == 0){
        return [NSString stringWithFormat:@"Овуляция началась"];
    }
    else  //No need to compare here
        return [NSString stringWithFormat:@"Овуляция началась"];
    }
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
2

Since the final condition is the exhaustive case, change the final else if to an else and call it a day. If you want, add your condition as a same-line comment to keep it clear.

The end result would be something like this:

- (NSString *)getNextOvulationString
{
    if (self.daysToNextOvulation < 0)
    {
        return [NSString stringWithFormat:@"Овуляция была %li дней назад", labs(self.daysToNextOvulation)];
    }   
    else if (self.daysToNextOvulation == 0)
    {
        return [NSString stringWithFormat:@"Овуляция началась"];
    }   
    else // self.daysToNextOvulation > 0
    {
        return [NSString stringWithFormat:@"Овуляция началась"];
    }
 }

You could also drop the final else altogether like this:

- (NSString *)getNextOvulationString
{
    if (self.daysToNextOvulation < 0)
    {
        return [NSString stringWithFormat:@"Овуляция была %li дней назад", labs(self.daysToNextOvulation)];
    }   
    else if (self.daysToNextOvulation == 0)
    {
        return [NSString stringWithFormat:@"Овуляция началась"];
    }   

    // self.daysToNextOvulation > 0
    return [NSString stringWithFormat:@"Овуляция началась"];
 }

And since the last two conditions both return the same thing you could even write it this way:

- (NSString *)getNextOvulationString
{
    if (self.daysToNextOvulation < 0)
    {
        return [NSString stringWithFormat:@"Овуляция была %li дней назад", labs(self.daysToNextOvulation)];
    }
    else // self.daysToNextOvulation >= 0
    {
        return [NSString stringWithFormat:@"Овуляция началась"];
    }
 }

Or this way, again dropping the else entirely:

- (NSString *)getNextOvulationString
{
    if (self.daysToNextOvulation < 0)
    {
        return [NSString stringWithFormat:@"Овуляция была %li дней назад", labs(self.daysToNextOvulation)];
    }

    // self.daysToNextOvulation >= 0
    return [NSString stringWithFormat:@"Овуляция началась"];
 }

Note: All of the above pieces of code are functionally equivalent given the current conditions and return values.

Beltalowda
  • 4,558
  • 2
  • 25
  • 33