Question

In the following code, I am passing an array that looks like this:

( AAAA|BBBBB|CCCCC|DDDDD|EEEEE )

This is just an element of another array.

I need to extract the first component of this element and return it as a string. Easy to do, you'd say. Yes, when I need to extract the 2nd, 3rd, 4th components. However when I try to extract the first component with the following code:

-(NSString*) navigationTypeIncludesSeparator : (NSMutableArray*) navPoint  {

   NSString* navigationTypeSelected;

   if ([navPoint isEqual: @"N/A"] || navPoint == nil)   
   {
       navigationTypeSelected = NAVDATA_DEFAULT;
   }

   else 
   {
       NSString* tempStr = [navPoint description];

       NSArray* arrayTemp = [tempStr componentsSeparatedByString:@"|"];

       navigationTypeSelected = [arrayTemp objectAtIndex:0];

       navigationTypeSelected = [navigationTypeSelected uppercaseString];
   }
   return navigationTypeSelected;
}

instead of AAAAA, I get:

"\n \AAAAA"

Is there anything I am missing?

Thank you!

Was it helpful?

Solution

You should not use description to get a string value from the array. This is meant to be printed in the Console or in a user interface, and will include strange things like line breaks and such.

If you are passed in an Array, can you just format the array so value 0 is "AAAA", value 1 is "BBBB" and so on? Or can you pass in the String "AAAA|BBBBB|CCCCC|DDDDD|EEEEE" instead? In my code if I do:

NSArray* arrayTemp = [@"AAAA|BBBBB|CCCCC|DDDDD|EEEEE" componentsSeparatedByString:@"|"];
NSString *navigationTypeSelected = [arrayTemp objectAtIndex:0];
navigationTypeSelected = [navigationTypeSelected uppercaseString];
NSLog(@"Answer: %@", navigationTypeSelected);

Then it works great. But you could just as easily pass in an array and ask for [someArray firstObject];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top