0

I have been trying to add an object as an NSMutableDictionary to my array, which I am accessing from another view, and It doesn't seem to work. I want to be able to store the data in a plist which I access from a NSDictionary.

-(void)saveAlarm:(id)sender {

    // Adding object for alarm to AlarmViewController

    alarmArrayCopy = alarmViewController.alarmsTime;

    NSMutableDictionary *newAlarm =  [[NSMutableDictionary alloc] init];
    [newAlarm setValue:labelTextField.text forKey:LABEL_KEY];
    [newAlarm setValue:alarmPicker.date forKey:TIME_KEY];
    [alarmArrayCopy addObject:(newAlarm)];

    // Dismissing and tiding up.

    [self.navigationController dismissModalViewControllerAnimated:YES];
    [newAlarm release];
}

UPDATE: How do I add an NSDictionary to my plist database (my db is an array)?

Here is some new code, I updated the NSMutableDictionary to NSDictionary because in my plist you can only have normal dictionaries not a mutable one. But now it crashed and gives me a Thread 1:Program received signal: "SIGABRT".

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"data.plist"];

// Adding object for alarm to AlarmViewController

NSDictionary *newAlarm =  [[NSDictionary alloc] init];
[newAlarm setValue:labelTextField.text forKey:LABEL_KEY];
[newAlarm setValue:[NSString stringWithFormat:@"%@", alarmPicker.date] forKey:TIME_KEY];
[newAlarm writeToFile:finalPath atomically:NO];

or

-(IBAction)saveAlarm:(id)sender {

// Adding object for alarm to AlarmViewController

NSString *time = [NSString stringWithFormat:@"%@", alarmPicker.date];
NSString *label = [NSString stringWithFormat:@"%@",labelTextField.text];

NSDictionary *newAlarm = [[NSDictionary alloc] initWithObjectsAndKeys:label, LABEL_KEY,time, TIME_KEY, nil];
self.alarmArrayCopy = alarmViewController.alarmsTime;
[alarmArrayCopy addObject:(newAlarm)];

// Dismissing and tiding up.
[newAlarm release];
[self.navigationController dismissModalViewControllerAnimated:YES];

}

Souljacker
  • 774
  • 1
  • 13
  • 35

2 Answers2

2

First, you should use setObject:forKey: method for adding objects to NSMutableDictionary. Second, you should use initWithObjectsAndKeys: method if you are using NSDictionary.

The setValue:forKey is a method of the Key Value Coding protocol. That was described at here “ Where's the difference between setObject:forKey: and setValue:forKey: in NSMutableDictionary?

So, you should do that,

NSDictionary *newAlarm = [[NSDictionary alloc] initWithObjectsAndKeys:
                                               labelTextField.text, LABEL_KEY,
                                               alarmPicker.date, TIME_KEY, nil];
Community
  • 1
  • 1
cockscomb
  • 996
  • 6
  • 3
  • thanks, this solved my problem but I have encountered a new problem. The data stored in the dictionary, how do I add it to my plist? I mean I have tried writeToFile and trying to add it to a array but I can't. I will put some new code up. – Souljacker Nov 02 '11 at 11:06
1
[newAlarm setValue:alarmPicker.date forKey:TIME_KEY];

I am not quite sure, but I guess your error is because you can't send an instance of NSDate object to setValue:forKey method. You may use either setObject:forKey or change NSDate to NSString by [NSString stringWithFormat:"%@", alarmPicker.date].

Hope that helps.

Sierra Alpha
  • 3,707
  • 4
  • 23
  • 36