0

Solution: Make the NSArray mutable with mutableCopy

I want to edit a NSDictionary in a NSArray in NSUserDefaults. That's a bigger problem than I thought :/ The property parentpop.dataseditable is (retain, nonatomic) and loads the NSArray with the code

dataseditable = (NSMutableArray*)[[NSUserDefaults standardUserDefaults]arrayForKey:(@"array")];

so there should be really no problem. I hope you can help me.

//  EditSUPER.m
//  HITS Datenbank
//
//  Created by Jonathan Lucas Fritz on 02.05.16.
//  Copyright © 2016 NOSCIO. All rights reserved.
//

#import "EditSUPER.h"
NSString *key_edit;
NSString *index_edit;
NSMutableDictionary *dica4;
UITextView *textview;
@implementation EditSUPER
-(void)loadandsavefor:(NSString *)key andindex:(NSString *)index
{
    self.backgroundColor = [UIColor blackColor];
    key_edit = key;
    index_edit = index;

    UIButton *back;
    UIButton *save;
    textview = [[UITextView alloc]initWithFrame:CGRectMake(0, 40, self.frame.size.width, self.frame.size.height-40)];
    textview.backgroundColor = [UIColor whiteColor];
    back = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
    save = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-160, 0, 150, 40)];

    [back setTitle:(@"Zurück") forState:UIControlStateNormal];
    [save setTitle:(@"Speichern") forState:UIControlStateNormal];
    [save setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [back setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [save setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [back setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [self addSubview:textview];
    [self addSubview:back];
    [self addSubview:save];
    [save addTarget:self action:@selector(savethat) forControlEvents:UIControlEventPrimaryActionTriggered];
    [back addTarget:self action:@selector(cancelediting) forControlEvents:UIControlEventPrimaryActionTriggered];

    dica4 = (NSMutableDictionary*)[self.parentpop.dataseditable objectAtIndex:index.intValue];
//    dica4 = (NSMutableDictionary*)_usethisdic;
    textview.text = [dica4 valueForKey:key_edit];

}
-(void)savethat //Save the Users Input and replace the old
{
    [textview resignFirstResponder];
    [dica4 setValue:textview.text forKey:key_edit];
    printf("\n %d ",index_edit.intValue);
    [_parentpop.dataseditable replaceObjectAtIndex:index_edit.intValue withObject:dica4];

    [[NSUserDefaults standardUserDefaults] setObject:_parentpop.dataseditable forKey:(@"array")];
    [[NSUserDefaults standardUserDefaults]synchronize];

    [self.parentpop removeFromSuperview];
//    [self.intclasssecond reloadData];

}
-(void)cancelediting //Cancel
{
    [textview resignFirstResponder];
    [self.parentpop removeFromSuperview];
}
@end
chronikum
  • 716
  • 5
  • 12
  • Casting NSArray to NSMutableArray doesn't make it mutable. Your comment "So there should be really no problem" marks exactly the spot in your code where you just shot yourself in the foot. – gnasher729 May 03 '16 at 07:53
  • Yep, i saw that ^^ – chronikum May 03 '16 at 07:53
  • You should really get used to using camelCase in your method names properly. Objective-C actually relies on your method names and you can get strange results if you don't follow the rules. – StilesCrisis May 03 '16 at 08:01
  • Excuse me, what is camelCase? :) I haven't heard that yet. – chronikum May 03 '16 at 08:03

0 Answers0