0

I want to parse the string below so that each row is put into an array.

I have the following string as a NSString:

(
"2 ripe avocados",
"1/2 small onion, minced",
"1 clove garlic, minced",
"1 small jalape\U00f1o, stems and seeds removed, minced",
"2 tablespoons cilantro leaves, finely chopped",
"1 tablespoon of fresh lime juice",
"1/2 teaspoon coarse salt",
"A dash of freshly grated black pepper",
"1 Roma tomato, chopped",
"4 slices crusty white bread",
"4 slices Cheddar cheese",
"Butter, for buttering bread"
)

I tried the following:

NSCharacterSet* charset = [NSCharacterSet characterSetWithCharactersInString:@"()"];
   NSString *newStr = _recipe.ingredients;
   newStr = [newStr stringByTrimmingCharactersInSet:charset];
   NSArray* array = [newStr componentsSeparatedByString:@"\","];
   NSCharacterSet* charset2 = [NSCharacterSet characterSetWithCharactersInString:@"\""];
   NSString *ingredientString = [[array objectAtIndex:0] stringByTrimmingCharactersInSet:charset2];

   NSLog(@"%@", ingredientString);

But I get a lldb error:

-[__NSCFArray stringByTrimmingCharactersInSet:]: unrecognized selector sent to instance 0x200a8d90

Do I need to retain the ingredients string?

wwjdm
  • 2,568
  • 7
  • 32
  • 61
  • 1
    Can you alter the input string slightly to make it proper json and use a json parser? – Kevin Jun 26 '13 at 00:20
  • Can you log your `array`? – ggrana Jun 26 '13 at 05:46
  • @ggrana Can't log array because newStr = [newStr stringByTrimmingCharactersInSet:charset]; causes lldb memory error – wwjdm Jun 26 '13 at 05:57
  • _recipe.ingredients is probably not an string... comment all the code and log the _recipe.igredients – ggrana Jun 26 '13 at 06:03
  • @ggrana NSSLog produces the same string above. IF it is not a string what could it be. I used if kindofclass and compared it to NSString and it was true. – wwjdm Jun 26 '13 at 06:14
  • So it is not possible that this error is happening in this line ... did you removed the second trim? What have you tried ? – ggrana Jun 26 '13 at 06:23
  • @ggrana I tried both. Anytime I call a method on the string I get the lldb error. Let me post more code so you can see how I retain the string data. – wwjdm Jun 26 '13 at 06:27

3 Answers3

1

If you have this exactly string in a NSString you can parse it to an array with this 3 lines:

    NSCharacterSet* charset = [NSCharacterSet characterSetWithCharactersInString:@"()"];
    str = [str stringByTrimmingCharactersInSet:charset];
    array = [str componentsSeparatedByString:@","];

The first line we create an characterser with the ( and ) chars, that we use in the second line to trim the string and remove it from the begining and to the end of the string.

The last line will create an array separing your components using , and you will have an array, that in the first position will contain the string "2 ripe avocados"

If it did not work the way you expect you can try something like:

    NSCharacterSet* charset = [NSCharacterSet characterSetWithCharactersInString:@"()"];
    str = [str stringByTrimmingCharactersInSet:charset];
    NSArray* array = [str componentsSeparatedByString:@"\","];
    NSCharacterSet* charset2 = [NSCharacterSet characterSetWithCharactersInString:@"\""];
    [[array objectAtIndex:0] stringByTrimmingCharactersInSet:charset2];

but the bases to solve your problem you already have. :)

Hope this help you.

ggrana
  • 2,335
  • 2
  • 21
  • 31
  • ok, I tried the first one and I get lldb memory issues for str = [str stringByTrimmingCharactersInSet:charset]. Not sure why. Also I will remove all white space. There are 5 spaces between each word. – wwjdm Jun 26 '13 at 00:57
  • If you can post more code I can help you more directly, but I think that it will not change a lot from what I posted, probably only some adjusts to match your case. – ggrana Jun 26 '13 at 01:22
0

Try NSString's -componentsSeparatedByString:. See here. It looks like you want to separate by ",", be sure that you've removed or escaped your speech marks and that you don't have any stray whitespace characters after each comma.

In fact, note that you have commas inside some components. Consider revising the string to use some other separating character, such as a semi-colon. Otherwise the results won't be what you want (for example "1 clove garlic, minced" splits in two components, not one).

Ken
  • 30,811
  • 34
  • 116
  • 155
0

I figured it out thanks to @ggrana. The object was not type String, it was type NSArray. I dont need to parse anymore. I checked this with

if ([idont isKindOfClass:[NSArray class]]){
 NSLog(@"*****************YEA %@", idont);
}
wwjdm
  • 2,568
  • 7
  • 32
  • 61