简体   繁体   English

如何在Objective -C XCode中比较两个String

[英]How to compare two String in Objective -C XCode

I am checking user credential from WebAPI userId and userPWD and checking them. 我正在从WebAPI userIduserPWD检查用户凭证,并检查它们。 If User is valid then he access the app. 如果用户有效,则他将访问该应用程序。 If not then I am showing AlertView Controller to user that he is not valid user. 如果不是,那么我向用户显示AlertView Controller,他不是有效用户。 How to do that. 怎么做。 Please give me the example. 请举个例子。

I post my code here: 我在这里发布代码:

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.

}

Response for its here : 在这里回应:

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

Try this : 尝试这个 :

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

== compares the address of the objects, not their content. ==比较对象的地址,而不是它们的内容。 Two different objects will obviously never have the same address. 两个不同的对象显然永远不会具有相同的地址。 To compare strings use NSString's isEqualToString: method: 要比较字符串,请使用NSString的isEqualToString:方法:

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

} }

Note the square brackets [ ]. 请注意方括号[]。 This is the proper Objective-C syntax for sending messages (ie calling functions). 这是用于发送消息(即调用函数)的正确的Objective-C语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM