开发者

iphone response [closed]

开发者_StackOverflow This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

i am getting response like {"nok"} .... now i want to compare this for check status of data sent or not.

this is my code

   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];

       }

it's always run else part...

please help me


USe the below function of NSString.

- (NSRange)rangeOfString:(NSString *)aString

The above function will not check for the equality, it will tell u whether your responseStr contain "nok" string OR not ..

    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];

       }

    }


Try:

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


Try replacing

responseStr == @"nok"

with

[responseStr isEqualToString:@"nok"];

In your code, the variable is not checking if the characters in the string are the same, it is checking if they are pointing to the same location in memory.


Try isEqualToString

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

}

or compare

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

}

Never compare 2 strings using == operator.


Try.

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

Or you can remove all exara characters from response string and then compair it.

Like -

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

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

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

Or you can try.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜