Xcode编译错误"Control reaches end of non-void function"

Xcode compilation error "Control reaches end of non-void function"

我写的脚本有错误,错误是:

Control reaches end of non-void function

这是我的代码:

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

谁能看出我哪里出错了?

您的方法旨在使用 return 语句 return 布尔值,例如 return YES;.

因为你没有实现这样的东西,这个方法不会编译成功。如果你想 return 一个 BOOL 你可以添加一个 return 语句。如果你不想return一个BOOL只需更改方法初始化:

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

    }