您好我已经设置了一个表视图来显示plist中的entires并且有一个部分来向plist添加新数据但我无法保存数据...它存储正常并且显示正常但是当我离开时应用程序和重新加载新数据已经在这里是如何保存
- (IBAction) save: (id) sender {
NSLog(@"%@", @"Save pressed!");
if (self.job != nil) {
// We're working with an existing drink, so let's remove
// it from the array and just recreate it like we would a new
// drink...
[jobArray_ removeObject:self.job];
self.job = nil; // This will release our reference and set the property to nil
}
// Create a new drink dictionary for the new values
NSMutableDictionary *newDrink = [[NSMutableDictionary alloc] init];
[newDrink setValue:self.nameTextField.text forKey:NAME_KEY];
[newDrink setValue:self.ingredientsTextView.text forKey:INGREDIENTS_KEY];
[newDrink setValue:self.directionsTextView.text forKey:DIRECTIONS_KEY];
// Add it to the master drink array and release our reference
[jobArray_ addObject:newDrink];
// Sort the array since we just added a new drink
NSSortDescriptor *nameSorter = [[NSSortDescriptor alloc] initWithKey:NAME_KEY ascending:YES selector:@selector(caseInsensitiveCompare:)];
[jobArray_ sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
// Pop the modal view and go back to the list
[self dismissViewControllerAnimated:YES completion:NULL];
}
我是否犯了一个错误,因为它没有保存到plist?它加载我在plist中预设的数据,但不加载新的entrys
提前致谢
答案 0 :(得分:1)
请将数据保存在pList中,如下所示:
NSMutableDictionary *dict;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *plistPath = [documentPath stringByAppendingPathComponent:@"myplist.plist"];
NSLog(@"File path = %@", plistPath);
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:plistPath];
// This line saves the dictionary to pList
[dict writeToFile:plistPath atomically:YES];