2017-03-04 60 views
0

我正在检查WebAPI userIduserPWD的用户凭据并检查它们。如果用户有效,那么他访问该应用程序。如果没有,那么我向用户显示AlertView控制器,他不是有效的用户。怎么做。 请给我一个例子。如何比较Objective-C中的两个字符串

我在这里发布我的代码:

为其这里
if([jstr isEqualToString:@"Invalide Login Credential."]) 
{ 
    UIAlertController *alert= 
      [UIAlertController alertControllerWithTitle:@"Enter valid Details." message:@"invalid" preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction* ok = 
      [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) 
    {                   
     [alert dismissViewControllerAnimated:YES completion:nil];                   
    }]; 
    [alert addAction:ok]; 

    [self presentViewController:alert animated:YES completion:nil]; 

    NSLog(@"Invalide Login Credential."); 


    //Invalide Login Credential. 

} 

响应:

Jstr = Invalide Login Credential. 
+0

你可以显示你的json解析,否则添加一些额外的代码 –

+0

我认为这是错的jstr –

+0

代码是正确的,使用isEqualToString来比较两个字符串。 如果不起作用,请在代码的第一行插入断点(或NSLog)并检查jstr值。 – Kerberos

回答

0
if([jstr isEqualToString:@"Invalide Login Credential."]) 
{ 
    //Invalide Login Credential. with UIAlertController 
} 
else 
{ 
    //valid user, do whatever you want the conditions here 
} 
+0

尽管此代码可能会回答这个问题,但提供有关如何解决问题和/或为何解决问题的其他上下文会提高答案的长期价值。 –

0

试试这个:

if ([jstr rangeOfString:@"Invalide Login Credential." options:NSCaseInsensitiveSearch].location == NSNotFound) { 
     // show error alertViewControler 
} 
else { 
    NSLog(@"matched"); 
} 
0

==对象的地址进行比较,而不是他们的内容。两个不同的对象显然永远不会有相同的地址。 比较字符串使用NSString的isEqualToString:方法:

if ([string1 isEqualToString:string2]) { 
NSLog(@"it is equal"); 

}

注意的方括号[]。这是发送消息的正确Objective-C语法(即调用函数)。