Plist文件路径错误?

发布于 2024-10-17 12:47:27 字数 1589 浏览 6 评论 0原文

我正在尝试使用以下代码将包含文本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2024-10-24 12:47:27

首先尝试初始化数组:

array = [[NSMutableArray alloc] initWithContentsOfFile:file];

Try initialising your array first:

array = [[NSMutableArray alloc] initWithContentsOfFile:file];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文