iphone的回应

时间:2011-05-09 06:19:52

标签: ios cocoa-touch

我收到像{“nok”}这样的回复....现在我想比较一下是否发送数据的检查状态。
这是我的代码

   NSString *responseStr = [sender responseString];


 NSLog(@"response string %@", responseStr);
if([responseStr isEqualToString:@"nok"])
 {
    NSLog(@"Hiii");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Attenzione: si è verificato un errore di richiesta del premio." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];
}
  else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Inviato con successo" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];
    [self startIscrivity2View];

       }

它总是运行其他部分...

请帮帮我

5 个答案:

答案 0 :(得分:1)

使用 NSString 的以下功能。

- (NSRange)rangeOfString:(NSString *)aString

上述函数不会检查是否相等,它会告诉你 responseStr 是否包含“nok”字符串或者不是..

    NSRange myStringRange = [responseStr rangeOfString:@"nok"] ;
    if(myStringRange.location != NSNotFound )
    {
      NSLog(@"Hiii");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Attenzione: si è v      erificato un errore di richiesta del premio." delegate:self cancelButtonTitle:nil  otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];
     }
  else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Inviato con successo" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];
    [self startIscrivity2View];

       }

    }

答案 1 :(得分:0)

尝试:

if([responseStr isEqualToString: @"nok"])
{
   ...
}

答案 2 :(得分:0)

尝试替换

responseStr == @"nok"

[responseStr isEqualToString:@"nok"];

在你的代码中,变量不检查字符串中的字符是否相同,而是检查它们是否指向内存中的相同位置。

答案 3 :(得分:0)

尝试isEqualToString

if([responseStr isEqualToString:@"nok"]){

}

或比较

if([responseStr compare:@"nok"] == NSOrderedSame){

}

绝不使用==运算符比较2个字符串。

答案 4 :(得分:0)

尝试。

if([responseStr isEqualToString: @"{\"nok\"}"] == TRUE)
{
 ....
}
else
{
  .....
}

或者您可以从响应字符串中删除所有额外字符,然后进行比较。

喜欢 -

NSString *str = [responseStr stringByReplacingOccurrencesOfString:@"{" withString:responseStr];

str = [responseStr stringByReplacingOccurrencesOfString:@"}" withString:responseStr];
str = [responseStr stringByReplacingOccurrencesOfString:@"\"" withString:responseStr];

if([responseStr isEqualToString: @"nok"] == TRUE)
{
     ....
}
else
{
      .....
}

或者你可以试试。

NSRange myStringRange = [responseStr rangeOfString:@"nok"] ;
if(myStringRange.location != NSNotFound )
{
  ...
}
else
{
  ...
}