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?
Starting with XCode 4.5 (and Clang 3.3), you may use Objective-C Literals:
NSString *tmpString1 = tempArray[1];
Its just simply [array objectAtIndex:0]
in Objective-C ;-)
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...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With