比较NSString使用isEqualToString

问题描述:

请问,为什么不isEqualToString工作?比较NSString使用isEqualToString

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    extern NSString* globalpassword; 

    if ([passwo.text isEqualToString: globalpassword]) { 

     res = [[XMLTestViewController alloc] initWithNibName:@"XMLTestViewController" bundle:nil]; 
     res.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
     [self.view addSubview:res.view]; 
    } else { 

     NSLog(@"faux"); 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"chikos" 
                 message:@"Wrong Password" delegate:self cancelButtonTitle:@"Cancel" 
               otherButtonTitles:@"OK", nil]; 

     [alert show]; 
     [alert release]; 
    } 
} 


the result is          
2011-08-11 17:56:36.543 XMLTest[6389:207] pol 
2011-08-11 17:56:36.544 XMLTest[6389:207] pol 
2011-08-11 17:56:36.544 XMLTest[6389:207] faux 
+0

你为什么用'stringWithString'而不是''[passwo.text isEqualToString:globalpassword]''来烦恼? – deanWombourne

+0

我尝试比较UIText字段和我的globalpassord但不工作 –

+0

编辑您的问题并添加一些有关错误的信息 - 崩溃了吗? – deanWombourne

看看你的NSLog行;它应该是这样的:

NSLog(@"hi all freind"); 

(注意@符号!)


编辑这一定意味着你忽略你的编译器警告!他们在那里是有原因的;在这种情况下,告诉你,你正在向NSLog传入错误的参数。

我敢打赌,你也会在NSLog(cc)NSLog(bb)行上发出警告吗?

+0

哦,很好的观察! –

+0

其实,我更喜欢你的答案 - 编译器可以修复我看到的错误,但它需要经验来修复你捕获的错误;) – deanWombourne

+0

好吧,我纠正它们,但我不能比较我的UITextFIled中的值 –

字符串不同。尝试登录BB和CC这样的:

NSLog(@"bb: '%@', cc: '%@'", bb, cc); 

用单引号就可以看到是否有任何BB或CC意外空白。