Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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

iphone string

  • Please log in to reply
3 replies to this topic

#1 Alhazred

Alhazred

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 133 posts

Posted 28 June 2011 - 06:31 AM

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?
  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 28 June 2011 - 06:40 AM

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.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 Alhazred

Alhazred

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 133 posts

Posted 28 June 2011 - 07:00 AM

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.
  • 0

#4 Alhazred

Alhazred

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 133 posts

Posted 28 June 2011 - 07:05 AM

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"]) {
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download