检查函数是否返回true Objective-C

时间:2017-01-18 10:33:50

标签: objective-c

我想检查当函数返回真值然后NSLog(@"hello!!!");只是为了确认,我想检查validUrl是否返回true。我已经向控制器声明它在里面,这是完整的代码:

NewInAppWebViewController *webViewController = [[NewInAppWebViewController alloc] init];

        if([webViewController validUrl:(NSURL *)url] == true) {
            NSLog(@"hello!!!");
        }
        else
        {
            [self showAlertForExternalURL:url];
        }

以下是功能:

- (BOOL)validUrl:(NSURL*)url {
    NSString *stringURL = [url absoluteString];

    if([stringURL length]==0){
        return false;
    }

    NSString *regExPattern = @"www-(test|testing[a-z]|newtest)\.testerer\.com";

    NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSUInteger regExMatches = [regEx numberOfMatchesInString:stringURL options:0 range:NSMakeRange(0, [stringURL length])];

    NSLog(@"%i", regExMatches);
    if (regExMatches == 0) {
        return false;
    } else {
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

NSURL传递给方法参数并调用方法如下:

NewInAppWebViewController *webViewController = [[NewInAppWebViewController alloc] init];

NSURL *url=yourNSURLHere;

  if([webViewController validUrl:url]) {

     NSLog(@"hello!!!");
  }
  else{

     [self showAlertForExternalURL:url];
  }