14

Hey everbody, i'm trying to convert one string to an array. My php is setting this:

echo "Logged/".$name;

So, how can i take 'Logged' and the 'Name' as two differents strings? And how can i call it?

Thanks!

1
  • 1
    Yes this is PHP. Where's the Objective-C? Commented Nov 3, 2010 at 19:05

1 Answer 1

44

If you read your string like: "logged/name" into an NSString, you can use

- (NSArray *)componentsSeparatedByString:(NSString *)separator

to split it, like:

NSString *list = @"logged/name";

NSArray *listItems = [list componentsSeparatedByString:@"/"];

Would produce an NSArray of two NSStrings:

[ @"logged", @"name" ]
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, thats it. But, how can i access this string? The name i mean, is a dynamic string provided by php.
NSString *name = [listItems lastObject]; or [listItems objectAtIndex:1];
NSLog(@"There are %d strings - the first is %@ and the last is %@",[listItems count],[listItems objectAtIndex:0],[listItems lastObject]);
@Lucas Veiga: You would do a URL request to whatever server the PHP file was on and use the result string.
You could do that - but if you're doing what I think you're doing - the better way to do it is to spit your data out of your PHP page as XML data, then use NSXMLParser to read and parse the data from PHP. If you only have one string you're dealing with you might not need to - but if you had more data - I'd look into doing it that way...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.