wwjdm
wwjdm

Reputation: 2596

ios: Parse String to Array

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?

Upvotes: 0

Views: 926

Answers (3)

wwjdm
wwjdm

Reputation: 2596

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);
}

Upvotes: 0

ggrana
ggrana

Reputation: 2335

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.

Upvotes: 1

Ken
Ken

Reputation: 31161

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).

Upvotes: 0

Related Questions