views:

44

answers:

1

I have a StateArray.plist set up with 50 dictionary entries each containing a string with the name of the state and a number 0. What I want to be able to do is change the 0 to a 1 through a segmented control and save the updated plist. The code I have is below and won't save the changed number.

-(void) viewWillAppear: (BOOL) animated 
{
 [super viewWillAppear:animated];
 nameOfStateTextField.text = [states objectForKey:NAME_KEY];
}

//segmented control
-(IBAction)visitedChanged
{
 selectedVisited = visitedSegmentedControl.selectedSegmentIndex;
}


-(IBAction)saveButton
{
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *path = [documentsDirectory stringByAppendingPathComponent:@"StateArray.plist"];
 [states setValue:selectedVisited forKey:THERE_KEY];
 [states writeToFile:path atomically:YES];
}

The changed segmented control value should end up as the new THERE_KEY and be written to the Documents directory and saved. What could be wrong here?

+1  A: 

You need to put a NSNumber Object into the Dictionary. What datatype is "selectedVisited"?, i guess you don't get an error at this line:

 selectedVisited = visitedSegmentedControl.selectedSegmentIndex;

the datatype you choose is probably a NSInteger. (Since selectedSegmentIndex is a NSInteger).

You need to put the value into the dictionary something like this:

NSNumber *newValue = [[NSNumber alloc] initWithInteger:selectedVisited];
[states setValue:newValue forKey:THERE_KEY];
[newValue release];
thatsdisgusting