Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grabbing a specific element of an array in Objective-C

Tags:

objective-c

I'm splitting a string by ';', but want to specifically grab the first and second element.

I know with PHP it's just simply $array[0], just can't find anything for this for Objective-C

NSArray *tempArray = [returnString componentsSeparatedByString:@";"];

So here I have assigned my array, how can I go about getting the first and second element?

like image 635
9miles Avatar asked Sep 30 '11 11:09

9miles


3 Answers

Starting with XCode 4.5 (and Clang 3.3), you may use Objective-C Literals:

NSString *tmpString1 = tempArray[1];
like image 128
Andreas Ley Avatar answered Nov 15 '22 01:11

Andreas Ley


Its just simply [array objectAtIndex:0] in Objective-C ;-)

like image 17
EmptyStack Avatar answered Nov 15 '22 03:11

EmptyStack


NSString *tmpString = [tempArray objectAtIndex:0];
NSLog(@"String at index 0 = %@", tmpString);
NSString *tmpString1 = [tempArray objectAtIndex:1];
NSLog(@"String at index 1 = %@", tmpString1);

You may also wish to do an IF statement to check tmpArray actually contains objects in before attempting to grab its value...

e.g.

if ([tempArray count] >= 2) {

// do the above...

}

like image 2
Dan H Avatar answered Nov 15 '22 01:11

Dan H