NSMutableArray und NSMutableDictionary

  • NSMutableArray und NSMutableDictionary

    Ich möchte in ein dictionary array einfügen, was geht, aber die alten werden immer einfach überschrieben.
    Ich habe echt keine Ahnung wieso das nicht funktioniert, denn das mit der Erkennung ob der Wert gleich ist geht ja und wird in dem array abgespeichert. Das Array wird ins Dictionary eingefügt, aber es überschreibt alles!!!
    Was mache ich da falsch?

    ViewController.m:

    Quellcode

    1. #import "ViewController.h"
    2. @interface ViewController ()
    3. @end
    4. @implementation ViewController
    5. @synthesize textfeld,eingabe,liste;
    6. - (void)viewDidLoad
    7. {
    8. [super viewDidLoad];
    9. // Do any additional setup after loading the view, typically from a nib.
    10. array = [[NSMutableArray alloc] init];
    11. dict = [[NSMutableDictionary alloc] init];
    12. liste.text = @"1\n1\n1\n2\n2\n3\n3\n3\n3\n4\n4\n5\n5";
    13. [array addObject:@"1"];
    14. [array addObject:@"1"];
    15. [array addObject:@"1"];
    16. [array addObject:@"2"];
    17. [array addObject:@"2"];
    18. [array addObject:@"3"];
    19. [array addObject:@"3"];
    20. [array addObject:@"3"];
    21. [array addObject:@"3"];
    22. [array addObject:@"4"];
    23. [array addObject:@"5"];
    24. [array addObject:@"5"];
    25. }
    26. -(BOOL)textFieldShouldReturn:(UITextField *)textField{
    27. [textField resignFirstResponder];
    28. return YES;
    29. }
    30. - (void)didReceiveMemoryWarning
    31. {
    32. [super didReceiveMemoryWarning];
    33. // Dispose of any resources that can be recreated.
    34. }
    35. - (IBAction)add:(id)sender {
    36. liste.text = [NSString stringWithFormat:@"%@\n%@",liste.text,eingabe.text];
    37. //[array addObject:eingabe.text];
    38. }
    39. - (IBAction)save:(id)sender {
    40. NSMutableArray *arrayBig = [[NSMutableArray alloc] initWithArray:array];
    41. [array removeAllObjects];
    42. int count = 0;
    43. for (int i = 0; i<arrayBig.count; i++) {
    44. if (array.count==0) {[array addObject:[arrayBig objectAtIndex:i]];} else {
    45. if ([[arrayBig objectAtIndex:i] isEqualToString:[array objectAtIndex:array.count-1]]) {
    46. [array addObject:[arrayBig objectAtIndex:i]];
    47. } else {
    48. [dict setObject:array forKey:[NSNumber numberWithInt:count]]; NSLog(@"dict-1:%@",dict);
    49. count++;
    50. [array removeAllObjects];
    51. }
    52. }
    53. }
    54. }
    55. - (IBAction)show:(id)sender {
    56. NSLog(@"dict: %@",dict);
    57. }
    58. @end
    Alles anzeigen


    ViewController.h:

    Quellcode

    1. #import <UIKit/UIKit.h>
    2. @interface ViewController : UIViewController <UITextFieldDelegate> {
    3. @private NSMutableDictionary *dict;
    4. @private NSMutableArray *array;
    5. }
    6. @property (nonatomic, weak) UILabel *textfed;
    7. @property (weak, nonatomic) IBOutlet UILabel *textfeld;
    8. - (IBAction)show:(id)sender;
    9. - (IBAction)add:(id)sender;
    10. - (IBAction)save:(id)sender;
    11. @property (weak, nonatomic) IBOutlet UITextField *eingabe;
    12. @property (weak, nonatomic) IBOutlet UITextView *liste;
    13. @end
    Alles anzeigen


    und das ist die Ausgabe dazu:

    Quellcode

    1. 2013-09-16 19:44:51.317 savedictinary#[4682:c07] dict-1:{
    2. 0 = (
    3. 1,
    4. 1,
    5. 1
    6. );
    7. }
    8. 2013-09-16 19:44:51.319 savedictinary#[4682:c07] dict-1:{
    9. 0 = (
    10. 2
    11. );
    12. 1 = (
    13. 2
    14. );
    15. }
    16. 2013-09-16 19:44:51.320 savedictinary#[4682:c07] dict-1:{
    17. 0 = (
    18. 3,
    19. 3,
    20. 3
    21. );
    22. 1 = (
    23. 3,
    24. 3,
    25. 3
    26. );
    27. 2 = (
    28. 3,
    29. 3,
    30. 3
    31. );
    32. }
    33. 2013-09-16 19:44:51.935 savedictinary#[4682:c07] dict: {
    34. 0 = (
    35. 5,
    36. 5
    37. );
    38. 1 = (
    39. 5,
    40. 5
    41. );
    42. 2 = (
    43. 5,
    44. 5
    45. );
    46. }
    Alles anzeigen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von SpeedyBrainy ()

  • SpeedyBrainy schrieb:

    Ich möchte in ein dictionary array einfügen, was geht, aber die alten werden immer einfach überschrieben.

    Richtig.

    SpeedyBrainy schrieb:

    Ich habe echt keine Ahnung wieso das nicht funktioniert, denn das mit der Erkennung ob der Wert gleich ist geht ja und wird in dem array abgespeichert. Das Array wird ins Dictionary eingefügt, aber es überschreibt alles!!!

    Richtig.

    SpeedyBrainy schrieb:

    Was mache ich da falsch?

    Du überschreibst die Werte in deinem Dictionary.
    Genauer gesagt: du löscht die Werte aus dem Array in deinem Dictionary.

    Dieser Codeabschnitt ist dafür verantwortlich:

    C-Quellcode

    1. [dict setObject:array forKey:[NSNumber numberWithInt:count]]; NSLog(@"dict-1:%@",dict);
    2. count++;
    3. [array removeAllObjects];

    Du wirfst eine Referenz auf dein Array in das Dictionary. Du weist eine anderen Referenz desselben Arrays an, alle Objekte wegzuwerfen. Die Referenz deines Arrays zeigt also nun auf das leere Array.
    Vermutlich möchtest du eine Kopie des Arrays haben.

    C-Quellcode

    1. [dict setObject:[array copy] forKey:[NSNumber numberWithInt:count]]; NSLog(@"dict-1:%@",dict);
    2. count++;


    [Thallius]
    Was das ganze werden soll, wenn es mal fertig ist, weiß ich auch nicht.
    «Applejack» "Don't you use your fancy mathematics to muddle the issue!"

    Iä-86! Iä-64! Awavauatsh fthagn!

    kmr schrieb:

    Ach, Du bist auch so ein leichtgläubiger Zeitgenosse, der alles glaubt, was irgendwelche Typen vor sich hin brabbeln. :-P
  • SpeedyBrainy schrieb:

    Ich vergleiche einfach die Werte aus dem großen Pool (arrayBig) mit den Werten aus array (welches immer aus dem gleichen Werten besteht)

    Nein, mit dem zuvor gesetzten Wert. ;)

    Mit folgender Liste würde dein System zusammenbrechen:

    C-Quellcode

    1. liste.text = @"0\n1\n1\n1\n2\n2\n0\n3\n3\n0\n3\n3\n4\n0\n4\n5\n0\n5";
    «Applejack» "Don't you use your fancy mathematics to muddle the issue!"

    Iä-86! Iä-64! Awavauatsh fthagn!

    kmr schrieb:

    Ach, Du bist auch so ein leichtgläubiger Zeitgenosse, der alles glaubt, was irgendwelche Typen vor sich hin brabbeln. :-P
  • Lucas de Vil schrieb:


    Du wirfst eine Referenz auf dein Array in das Dictionary. Du weist eine anderen Referenz desselben Arrays an, alle Objekte wegzuwerfen. Die Referenz deines Arrays zeigt also nun auf das leere Array.
    Vermutlich möchtest du eine Kopie des Arrays haben.

    C-Quellcode

    1. [dict setObject:[array copy] forKey:[NSNumber numberWithInt:count]]; NSLog(@"dict-1:%@",dict);
    2. count++;


    [Thallius]
    Was das ganze werden soll, wenn es mal fertig ist, weiß ich auch nicht.

    DANKE!!!!!!!!!!!!!!!!!!!

    Und das ist nur ein Testprogramm, damit ich mal lerne arrays in Dictionarys einzufügen :)