Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving an object to array using NSMutableDictionary
    text
    copied!<p>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.</p> <pre><code>-(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]; } </code></pre> <p><strong>UPDATE: How do I add an NSDictionary to my plist database (my db is an array)?</strong></p> <p>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 <code>Thread 1:Program received signal: "SIGABRT".</code></p> <pre><code>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]; </code></pre> <p>or</p> <p>-(IBAction)saveAlarm:(id)sender {</p> <pre><code>// 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]; </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload