0

这在过去的一天变得非常令人沮丧,而且有点太多了:为什么这不起作用?

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",kAllScoresUnorderedArray]];

 NSMutableDictionary *test = [[NSMutableDictionary alloc] init];
 [test setObject:@"bob" forKey:@"tmo"];

 [test writeToFile:path atomically: YES];

我以前用过它,而且效果很好:我忘记在 info.plist 中添加什么了吗?这是我唯一能想到的。

感谢您的帮助!

编辑:也许它与 Xcode 4.5 和 iOS6 有关?它适用于 4.3/iOS5

4

1 回答 1

0

您是否已将 .plist 文件添加到您的项目中?如果是这样,试试这个:

NSError *error;
        NSFileManager *fileManager = [[NSFileManager alloc] init];
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",kAllScoresUnorderedArray]];

    if (![fileManager fileExistsAtPath: path])
            {
                NSString *bundle = [[NSBundle mainBundle] pathForResource:kAllScoresUnorderedArray ofType:@"plist"];

                [fileManager copyItemAtPath:bundle toPath: path error:&error];
            }

     NSMutableDictionary *test = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
     [test setObject:@"bob" forKey:@"tmo"];

     [test writeToFile:path atomically: YES];

如果没有,请在您的项目中创建属性列表。

于 2013-04-03T15:14:37.123 回答