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:
Alles anzeigen
ViewController.h:
Alles anzeigen
und das ist die Ausgabe dazu:
Alles anzeigen
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
- #import "ViewController.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- @synthesize textfeld,eingabe,liste;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- array = [[NSMutableArray alloc] init];
- dict = [[NSMutableDictionary alloc] init];
- liste.text = @"1\n1\n1\n2\n2\n3\n3\n3\n3\n4\n4\n5\n5";
- [array addObject:@"1"];
- [array addObject:@"1"];
- [array addObject:@"1"];
- [array addObject:@"2"];
- [array addObject:@"2"];
- [array addObject:@"3"];
- [array addObject:@"3"];
- [array addObject:@"3"];
- [array addObject:@"3"];
- [array addObject:@"4"];
- [array addObject:@"5"];
- [array addObject:@"5"];
- }
- -(BOOL)textFieldShouldReturn:(UITextField *)textField{
- [textField resignFirstResponder];
- return YES;
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)add:(id)sender {
- liste.text = [NSString stringWithFormat:@"%@\n%@",liste.text,eingabe.text];
- //[array addObject:eingabe.text];
- }
- - (IBAction)save:(id)sender {
- NSMutableArray *arrayBig = [[NSMutableArray alloc] initWithArray:array];
- [array removeAllObjects];
- int count = 0;
- for (int i = 0; i<arrayBig.count; i++) {
- if (array.count==0) {[array addObject:[arrayBig objectAtIndex:i]];} else {
- if ([[arrayBig objectAtIndex:i] isEqualToString:[array objectAtIndex:array.count-1]]) {
- [array addObject:[arrayBig objectAtIndex:i]];
- } else {
- [dict setObject:array forKey:[NSNumber numberWithInt:count]]; NSLog(@"dict-1:%@",dict);
- count++;
- [array removeAllObjects];
- }
- }
- }
- }
- - (IBAction)show:(id)sender {
- NSLog(@"dict: %@",dict);
- }
- @end
ViewController.h:
Quellcode
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController <UITextFieldDelegate> {
- @private NSMutableDictionary *dict;
- @private NSMutableArray *array;
- }
- @property (nonatomic, weak) UILabel *textfed;
- @property (weak, nonatomic) IBOutlet UILabel *textfeld;
- - (IBAction)show:(id)sender;
- - (IBAction)add:(id)sender;
- - (IBAction)save:(id)sender;
- @property (weak, nonatomic) IBOutlet UITextField *eingabe;
- @property (weak, nonatomic) IBOutlet UITextView *liste;
- @end
und das ist die Ausgabe dazu:
Quellcode
- 2013-09-16 19:44:51.317 savedictinary#[4682:c07] dict-1:{
- 0 = (
- 1,
- 1,
- 1
- );
- }
- 2013-09-16 19:44:51.319 savedictinary#[4682:c07] dict-1:{
- 0 = (
- 2
- );
- 1 = (
- 2
- );
- }
- 2013-09-16 19:44:51.320 savedictinary#[4682:c07] dict-1:{
- 0 = (
- 3,
- 3,
- 3
- );
- 1 = (
- 3,
- 3,
- 3
- );
- 2 = (
- 3,
- 3,
- 3
- );
- }
- 2013-09-16 19:44:51.935 savedictinary#[4682:c07] dict: {
- 0 = (
- 5,
- 5
- );
- 1 = (
- 5,
- 5
- );
- 2 = (
- 5,
- 5
- );
- }
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von SpeedyBrainy ()
