The following code writes the textField text to a plist successfully, and restores it when the app is turned off then on.
But when I uncomment the following two lines to add it to the persistence code ...
... then I get the following error:
I don't know what to change to get rid of this error. The text is word-for-word out of the book, Beginning iPhone Development by Dave Mark and Jeff LaMarche from Apress, except I am using my own field names.
Thanks,
Steve
But when I uncomment the following two lines to add it to the persistence code ...
Code:
// [array addObject:labelShockMountingTower.text];
// labelShockMountingTower.text = [array objectAtIndex:2];
... then I get the following error:
Code:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (2) beyond bounds (2)'
I don't know what to change to get rid of this error. The text is word-for-word out of the book, Beginning iPhone Development by Dave Mark and Jeff LaMarche from Apress, except I am using my own field names.
Thanks,
Steve
Code:
// Following is from chapter 11 of Beginning iPhone Development, for data persistence
-(NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
-(void)applicationWillTerminate:(NSNotification *)notification
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:textFieldRounded.text];
[array addObject:textFieldNotes2.text];
// [array addObject:labelShockMountingTower.text];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}
- (IBAction)segmentActionShockMountingTower:(id)sender
{
switch ([((UISegmentedControl *)sender) selectedSegmentIndex])
{
case 0:
labelShockMountingTower.text = NSLocalizedString(@"1", @"ButtonOption");
break;
case 1:
labelShockMountingTower.text = NSLocalizedString(@"2", @"ButtonOption");
break;
case 2:
labelShockMountingTower.text = NSLocalizedString(@"3", @"ButtonOption");
break;
default:
labelShockMountingTower.text = NSLocalizedString(@"2", @"ButtonOption");
break;
}
return;
}
- (void)viewDidLoad {
// ... misc. app code is here...
NSString *filePath = [self dataFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
textFieldRounded.text = [array objectAtIndex:0];
textFieldNotes2.text = [array objectAtIndex:1];
// labelShockMountingTower.text = [array objectAtIndex:2];
[array release];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification object:app];
[super viewDidLoad];
}