Jump to content

[Objective-c] How do I compare a string returned by a php script?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Alhazred

Alhazred

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
I'm just learning to develo applications for iPhone and I'm now following a tutorial.
It makes me send a request to a php script on a website and after that it writes in the console the string returned from the script.

I want to change a bit the app.
I want to write inside the console a text depending on the answer received from the php script.

By now I use this code to write inside the console
NSURLResponse* response;
NSError* error;
NSData* risultato = [NSURLConnection sendSynchronousRequest:richiesta returningResponse:&response error:&error];
    
NSLog(@"%@", [[[NSString alloc] initWithData:risultato encoding:NSASCIIStringEncoding] autorelease]);
The script returns "ok" or "ko", and I read it correctly inside the console.

I need to add something like
if("the response received from the script" is equal to "ok"){
   //text for ok
}
else {
   //text for ko
}
My problem is what to write for the "if" condition.
How can I make that comparison?

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Have you checked the Apple/other programming manuals on how NSString comparison can be done?

I would assume the following:
if([yourResponseString isEqualToString:@"ok"]) { 

   ...

}


Based on @"text" creating a valid NSString literal to compare to.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Alhazred

Alhazred

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
My problem is that apparently I don't have a NSString object, I have risultato which is a NSData object.
How do I read its value as a NSString? This is what I do not understand.

#4
Alhazred

Alhazred

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
I had the answer in front of my eyes and I didn't see it...

if ([[[[NSString alloc] initWithData:risultato encoding:NSASCIIStringEncoding] autorelease] isEqualToString:@"ok"]) {




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users