-2

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

   // 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];
4

1 回答 1

2

不。

一方面,它最后缺少一个右大括号,但也许你只是在将代码复制到问题中时忽略了它。

另一方面,代码读入字典;它不会从任何地方获得(任何东西的)数组,也不会写出任何东西(数组或其他任何东西)。

听起来你从某个地方复制了代码,希望它是你需要的。不要那样做。编写自己的代码。如果您愿意,请阅读代码,但只阅读它;不要只是将代码复制到您的程序中而不了解它的作用,也不要依赖其他人告诉您代码的作用。

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

显然,您还必须能够使用目标语言/框架编写代码。复制别人的代码不是替代品。充其量,您最终会得到一个不稳定或根本不起作用的劣质程序;在最坏的情况下(如果您作为承包商或员工“编程”),您将犯有剽窃罪。

于 2010-11-05T18:16:52.410 回答