iphone response [closed]
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
{
...
}
精彩评论