我有一个 plist,它是一个字典数组,我需要将其保存到文档目录中,此代码正确吗?

发布于 2024-10-01 00:01:35 字数 904 浏览 0 评论 0原文

下面的代码对于保存到磁盘是否正确?

   // get the path to the "Documents" directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// get the path to our plist ("Documents/foo.plist")
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"foo.plist"];

// read or create plist

NSMutableDictionary *dict;
// check if our plist already exists in the Documents directory...
NSFileManager *fileManager = [NSFileManager defaultManager];
if ( [fileManager fileExistsAtPath:plistPath] ) {
// ...if it does, read it
NSLog(@"dict existed, reading %@", plistPath);
dict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
} else {
// ...if it doesn't, create it
NSLog(@"dict didn't exist, creating...");
dict = [NSMutableDictionary dictionaryWithCapacity:1];

Is the code below correct for saving to disk?

   // get the path to the "Documents" directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// get the path to our plist ("Documents/foo.plist")
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"foo.plist"];

// read or create plist

NSMutableDictionary *dict;
// check if our plist already exists in the Documents directory...
NSFileManager *fileManager = [NSFileManager defaultManager];
if ( [fileManager fileExistsAtPath:plistPath] ) {
// ...if it does, read it
NSLog(@"dict existed, reading %@", plistPath);
dict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
} else {
// ...if it doesn't, create it
NSLog(@"dict didn't exist, creating...");
dict = [NSMutableDictionary dictionaryWithCapacity:1];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

等待圉鍢 2024-10-08 00:01:35

不。

一方面,它末尾缺少一个右大括号,但也许您在将代码复制到问题中时遗漏了它。

另一方面,代码读取字典;它不会从任何地方获取(任何内容)数组,也不会写出任何内容(数组或其他任何内容)。

听起来您像是从某处复制了代码,希望这是您所需要的。不要那样做。 编写自己的代码。如果需要,可以阅读代码,但仅阅读;不要在不了解代码用途的情况下将代码复制到程序中,也不要依赖其他人告诉您代码的用途。

为了成为任何语言或框架的程序员,您必须能够阅读该语言/框架的代码。阅读Objective-C 编程语言并阅读Cocoa 基础指南;一旦您了解了这些指南所教授的概念,剩下的就是练习阅读代码了。

显然,您还必须能够用目标语言/框架编写代码。复制别人的代码并不能替代。最好的情况是,您最终会得到一个劣质的程序,该程序不稳定或无法工作;在最坏的情况下(如果您作为承包商或雇员“编程”),您将犯有抄袭罪。

No.

For one thing, it's missing a closing brace at the end, but maybe you just left that out when copying the code into the question.

For another, the code reads in a dictionary; it does not get an array (of anything) from anywhere, nor does it write anything (array or anything else) out.

It sounds like you copied the code from somewhere, hoping that it's what you need. Don't do that. Write your own code. Read code if you want, but only read it; do not just copy code into your program without understanding what it does, and don't rely on other people to tell you what code does.

In order to be a programmer in any language or framework, you must be able to read code in that language/framework. Read the Objective-C Programming Language and read the Cocoa Fundamentals Guide; once you know the concepts those guides teach, all that remains is to practice reading code.

You must also, obviously, be able to write code in the target language/framework. Copying other people's code is not a substitute. At best, you will end up with a shoddy program that is flaky or just doesn't work; at worst (if you're “programming” as a contractor or employee), you will be guilty of plagiarism.

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