我想解析下面的字符串,以便將每行放入數組中。ios:將字符串解析爲數組
我有以下字符串作爲的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"
)
我試過如下:
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);
但我得到一個LLDB錯誤:
-[__NSCFArray stringByTrimmingCharactersInSet:]: unrecognized selector sent to instance 0x200a8d90
我需要保留配料串?
你可以稍微改變輸入字符串,使其適當json和使用json解析器? – Kevin
你可以登錄你的'數組'嗎? – ggrana
@ggrana無法記錄數組,因爲newStr = [newStr stringByTrimmingCharactersInSet:charset];導致lldb內存錯誤 – wwjdm