-1

我想我完全明白了。也许有人可以告诉我出了什么问题。

NSString:

Patrick\nNice picture!\nAndy\nI like it\nTim\nMe too\n

我想将此字符串存储为NSArray类似:

- NSArray
    - NSDictionary
           - name (e.g. Andy)
           - kommentar (e.g. I like it)

这在某种程度上是一个逻辑错误,但它让我发疯......(代码正在工作但没有做正确的事情。)

我的代码:

for (int i = 0; i <= ([[aStr componentsSeparatedByString:@"\n"] count] - 1 / 2); i++) {
    NSMutableDictionary * comment = [[NSMutableDictionary alloc] init];
    if (i % 2 == 0) {
        [comment setValue:[[aStr componentsSeparatedByString:@"\n"] objectAtIndex:i] forKey:@"name"];
        [comment setValue:[[aStr componentsSeparatedByString:@"\n"] objectAtIndex:i] forKey:@"kommentar"];
    }
    [commentsArray addObject:comment];
    //NSLog(@"%@", [comment description]);
    //NSLog(@"Bla.");
    //comment = nil;
}

for (int a = 0; a <= [commentsArray count] - 1; a++) {
    NSLog(@"Name: %@ Nachricht: %@", [[commentsArray objectAtIndex:a] valueForKey:@"name"], [[commentsArray objectAtIndex:a] valueForKey:@"kommentar"]);
}

我得到:

Name: Patrick Nachricht: Patrick
Name: (null) Nachricht: (null)
Name: Andy Nachricht: Andy
Name: (null) Nachricht: (null)
Name: Tim Nachricht: Tim
Name: (null) Nachricht: (null)
Name:  Nachricht: //that's because of the last \n
Name: (null) Nachricht: (null)

我也试过

[comment setValue:[[aStr componentsSeparatedByString:@"\n"] objectAtIndex:i + 1] forKey:@"kommentar"];

...但这给了我out of bounds- 错误,这很清楚。

解决方案:

 for (int i = 0; i <= ([[aStr componentsSeparatedByString:@"\n"] count] - 1); i++) {
    NSMutableDictionary * comment = [[NSMutableDictionary alloc] init];
    if (i % 2 != 0) {
        [comment setValue:[[aStr componentsSeparatedByString:@"\n"] objectAtIndex:i - 1] forKey:@"name"];
        [comment setValue:[[aStr componentsSeparatedByString:@"\n"] objectAtIndex:i] forKey:@"kommentar"];
    }
    [commentsArray addObject:comment];
}

for (int a = 0; a <= [commentsArray count] - 1; a++) {
    NSLog(@"Name: %@ Nachricht: %@", [[commentsArray objectAtIndex:a] valueForKey:@"name"], [[commentsArray objectAtIndex:a] valueForKey:@"kommentar"]);
}
4

2 回答 2

0

您确实需要更好地解释,但快速查看您的代码我会提出疑问。

1)。为什么你只循环一半数组?(count - 1)/2 在您给出的示例中,您只会阅读前两项。编辑 - 实际上用你的语法它会 1 / 2 然后 - 1...所以...

2)。即使不满足您对 i%2 = 0 的要求,为什么还要实例化您的评论对象并将其添加到您的数组中。

在我看来(一目了然)上面的示例将导致一个数组,其中数组中的每个 seconf 对象都是空的。将您的 add 放入 if 语句中。

于 2012-04-12T17:01:57.493 回答
0
for (int i = 0; i <= ([[aStr componentsSeparatedByString:@"\n"] count] - 1); i++) {
NSMutableDictionary * comment = [[NSMutableDictionary alloc] init];
if (i % 2 != 0) {
    [comment setValue:[[aStr componentsSeparatedByString:@"\n"] objectAtIndex:i - 1] forKey:@"name"];
    [comment setValue:[[aStr componentsSeparatedByString:@"\n"] objectAtIndex:i] forKey:@"kommentar"];
}
[commentsArray addObject:comment];
}

for (int a = 0; a <= [commentsArray count] - 1; a++) {
NSLog(@"Name: %@ Nachricht: %@", [[commentsArray objectAtIndex:a] valueForKey:@"name"], [[commentsArray objectAtIndex:a] valueForKey:@"kommentar"]);
}
于 2012-04-13T07:32:48.583 回答