2012-04-12 81 views
-1

我想我很懂。也許有人可以告訴我什麼是錯的。將NSString存儲到NSArray

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 - 誤差,這是顯而易見的。

SOLUTION:

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"]); 
} 
+0

通常情況下,在這樣的地方你的代碼是工作,但沒有做正確的事的情況下,我找到了解決辦法是使其工作**和**做正確的事情太多。 – yuji 2012-04-12 16:38:07

+0

所有我想提到的是,沒有語法錯誤,以便人們專注於邏輯方面。 – DAS 2012-04-12 16:39:33

+0

好的,那麼你也許認爲你可能很聰明地提及你的代碼實際上在做什麼而不是正確的事情? – yuji 2012-04-12 16:46:05

回答

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"]); 
} 
0

你真的需要更好地解釋,而是採取了快速看一下你的代碼,我會質疑。

1)。你爲什麼只循環一半數組? (count - 1)/ 2 在你給你的例子中只會讀到前兩項。編輯 - 實際上用你的語法,它會1/2然後 - 1 ...所以...

2)。爲什麼你要實例化你的註釋對象並將它添加到你的數組中,即使你的要求不滿足i%2 = 0。

在我看來(以一瞥)上面的例子將導致一個數組中的每個seconf對象都是空的。把你的添加到if語句中。

+0

看看我的解決方案。 2)你是對的:缺少if語句。 – DAS 2012-04-12 17:05:42