我正在尝试使用以下代码将包含文本的NSMutableArray保存到plist文件中:
- (IBAction)saveDoc:(id)sender
{
NSString *typedText = typingArea.text;
NSMutableArray *totalFiles = [[NSMutableArray alloc] initWithObjects:typedText, nil];
NSLog(@"Created: %@", totalFiles);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedFiles.plist"];
NSLog(@"Found Path: %@", path);
//[totalFiles addObject:typedText];
[totalFiles writeToFile:path atomically:YES];
NSLog(@"File Written: %@ to: %@", totalFiles, path);
[totalFiles release];
NSLog(@"Released: %@", totalFiles);
}
所有NSLog都按预期返回适当的值。我使用此代码来创建一个新的NSMutableArray与plist的内容:
- (void)viewDidLoad {
file = [[NSBundle mainBundle] pathForResource:@"savedFiles" ofType:@"plist"];
NSLog(@"Got Path: %@", file);
array = [NSMutableArray arrayWithContentsOfFile:file];
NSLog(@"Loaded Array into new array: %@", array);
//UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"fdsfasdf" message:[dic objectForKey:@"item1"] delegate:self cancelButtonTitle:@"ldfghjfd" otherButtonTitles:nil] autorelease];
//[alert show];
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
此代码中的NSLogs返回的路径与返回的第一个路径略有不同。它还说数组是(null)
我应该如何从plist文件中加载内容?
提前致谢,
泰特
答案 0 :(得分:1)
首先尝试初始化数组:
array = [[NSMutableArray alloc] initWithContentsOfFile:file];