0

I have an error in a script I am writing, the error is:

Control reaches end of non-void function

This is my code:

-(BOOL) hasInternet {
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.google.co.uk"];
    NetworkStatus internetStats = [reach currentReachabilityStatus];

    if (internetStats == NotReachable){
        UIAlertView *alertOne = [[UIAlertView alloc] initWithTitle:@"Internet" message:@"is DOWN" delegate:self cancelButtonTitle:@"Turn on your Internet" otherButtonTitles:@"Cancel",nil];
        [alertOne show];
    }
}

Can anyone see where I have gone wrong?

Guillaume Algis
  • 10,705
  • 6
  • 44
  • 72
DCJones
  • 3,121
  • 5
  • 31
  • 53
  • 1
    You do not return anything from a function that expects return value - exactly what compiler error tells you... – Vladimir Jan 29 '16 at 18:06

1 Answers1

1

Your method is designed to return a boolean value using a return statement, for example return YES;.

Since you haven't implemented such thing, the method won't compile successfully. If you want to return a BOOL you can add a return statement. If you don't want to return a BOOL just change the method initialization:

    -(void) hasInternet {

       Reachability *reach = [Reachability reachabilityWithHostName:@"www.google.co.uk"];
       NetworkStatus internetStats = [reach currentReachabilityStatus];

      if (internetStats == NotReachable){
         UIAlertView *alertOne = [[UIAlertView alloc] initWithTitle:@"Internet" message:@"is DOWN" delegate:self cancelButtonTitle:@"Turn on your Internet" otherButtonTitles:@"Cancel",nil];
         [alertOne show];
      }

    }
fredpi
  • 8,414
  • 5
  • 41
  • 61
  • I'm being pedantic, but it's not that the method "won't work". The value returned will [just be undefined](https://stackoverflow.com/questions/10079089/implicit-int-return-value-of-c-function) (though you'll most likely end up returning `0` implicitly). – Guillaume Algis Jan 29 '16 at 18:12
  • yeah, but since it's a syntax error, it won't even compile. – fredpi Jan 29 '16 at 18:15
  • Hi all, thanks for your help, Fixed following "return false" – DCJones Jan 29 '16 at 18:19
  • 1
    @returnfalse you're right. don't know why I assumed it would be a warning (not a _syntax_ error though ;) ). – Guillaume Algis Jan 29 '16 at 18:21