我有以下代码:
- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest{
strFinal = [theRequest responseString];
NSLog(@"Sattus Code : %@",[theRequest responseString]);
[self GoTo];
}
我正在使用ASIHttp进行登录。如果登录正常,则strFinal
设置为successfully
,否则设置为failed
。
我的GoTo
方法
-(void)GoTo{
NSString *failStr = @"failed";
NSString *successStr = @"successfully";
if (strFinal == successStr) {
//Navigate to other xib
}else
if (strFinal == failStr) {
UIAlertView *AlertFail = [[UIAlertView alloc] initWithTitle:@"LogIn Failed !" message:@"Wrong ID or Password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[AlertFail show];
}
}
}
问题是除非strFinal
失败,否则不会显示警报。
答案 0 :(得分:2)
更改:
if (strFinal == successStr) {
到
if ([strFinal isEqualToString:successStr]) {
答案 1 :(得分:1)
==运算符比较指针,即使它们的内容相同,它们通常也会不同。 isEqualToString方法比较它们的内容。所以请比较一下......
if ([failStr isEqualToString:successStr])
{
// ------- success
}
else
{
// ---- fail ------
}
答案 2 :(得分:1)
if (strFinal == failStr)
这不起作用。你必须使用isEqualToString。
if ([strFinal isEqualToString: failStr])
答案 3 :(得分:1)
只需编辑isEqualToString:from“=”符号。这是将2个NSStrings一起比较的正确方法。
-(void)GoTo{
NSString *failStr = @"failed";
NSString *successStr = @"successfully";
if ([strFinal isEqualToString:successStr]) {
//Navigate to other xib
}else
if ([strFinal isEqualToString:failStr]) {
UIAlertView *AlertFail = [[UIAlertView alloc] initWithTitle:@"LogIn Failed !" message:@"Wrong ID or Password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[AlertFail show];
}
}
希望这会有所帮助......
答案 4 :(得分:1)
要比较您需要使用的字符串isEqualToString: 以下是如何使用它的示例:
if ([strFinal isEqualToString: successStr])
{
// your code here
}
答案 5 :(得分:1)
像这样更改你的代码,然后检查
返回一个布尔值,该值使用基于Unicode的文字比较指示给定字符串是否等于接收者。
参数
ASTRING
用于比较接收器的字符串。
是 如果aString等同于接收者(如果他们具有相同的ID或者在文字比较中是NSOrderedSame),否则为NO。
比较使用字符串的规范表示,对于特定字符串,字符串的长度加上构成字符串的Unicode字符。当此方法比较两个字符串时,如果单个Unicodes相同,则字符串相等,而不管后备存储。应用于字符串比较时,“Literal”表示不应用各种Unicode分解规则,并且单独比较Unicode字符。因此,例如,“Ö”表示为组合字符序列“O”,而变音符号不会等于表示为一个Unicode字符的“Ö”。
当您知道两个对象都是字符串时,此方法比isEqual更快速地检查相等性:。
在OS X v10.0及更高版本中可用。
-(void)GoTo{
NSString *failStr = @"failed";
NSString *successStr = @"successfully";
if ([strFinal isEqualToString:successStr]) {
//Navigate to other xib
}else
if ([strFinal isEqualToString:failStr]) {
UIAlertView *AlertFail = [[UIAlertView alloc] initWithTitle:@"LogIn Failed !" message:@"Wrong ID or Password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[AlertFail show];
}
}
答案 6 :(得分:1)
你需要这样做
-(void)GoTo{
NSString *failStr = @"failed";
NSString *successStr = @"successfully";
if ([strFinal isEqualToString:successStr]) {
//Navigate to other xib
}else
if ([strFinal isEqualToString:failStr] ) {
UIAlertView *AlertFail = [[UIAlertView alloc] initWithTitle:@"LogIn Failed !" message:@"Wrong ID or Password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[AlertFail show];
}
}
答案 7 :(得分:0)
您可以尝试以下方式
if ([strFinal caseInsensitiveCompare:@"successfully"] == NSOrderedSame)
{
<#statements#>
}