Plist文件路径错误?
我正在尝试使用以下代码将包含文本的 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 按预期返回适当的值。我使用这段代码用 plist 的内容创建一个新的 NSMutableArray:
- (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 文件加载内容?
提前致谢,
泰特
I am trying to save an NSMutableArray containing text to a plist file using this code:
- (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);
}
All of the NSLogs return appropriate values as expected. I use this code to crate a new NSMutableArray with the contents of the 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;
}
The NSLogs in this code come back with a slightly different path than the first one returned with. It also said the array is (null)
How should I load the contents from a plist file?
Thanks in advance,
Tate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先尝试初始化数组:
array = [[NSMutableArray alloc] initWithContentsOfFile:file];
Try initialising your array first:
array = [[NSMutableArray alloc] initWithContentsOfFile:file];