0

我正在尝试将数组添加到我的 plist 中的 Root 数组中:

在此处输入图像描述

并且不工作。这是我的代码:

-(IBAction)addName:(id)sender{
NSArray *arrayValues = [NSArray arrayWithObjects: nameLabel.text, nameDate.text, nameValue.text, nil];
NSString *plistpath = [[NSBundle mainBundle] pathForResource:@"Names" ofType:@"plist"];
NSMutableArray *namesNew = [[NSMutableArray alloc] initWithContentsOfFile:plistpath];
[namesNew addObject:arrayValues];   
[namesNew writeToFile:plistpath atomically:YES];
}

我究竟做错了什么?谢谢!

4

3 回答 3

1

您需要将文件移动到 NSDocumentDirectory。然后编辑 plist 文件。

例如:

移动到 NSDocumentDirectory:

-(NSDictionary *)copyBundleToDocuments
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"Names.plist"];
    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:@"Names.plist"];

    //if file exists in the documents directory, get it
    if([fileManager fileExistsAtPath:documentPlistPath])
    {
        NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
        return documentDict;
    }
    //if file does not exist, create it from existing plist
    else
    {
        NSError *error;
        BOOL success = [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath error:&error];
        if (success) {
            NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
            return documentDict;
        }
        return nil;
    }
}

然后得到plist:

 -(void)plistArray:(NSArray*)array
    {
         //get the documents directory:
        NSArray *paths = NSSearchPathForDirectoriesInDomains
        (NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        //getting the plist file name:
        NSString *plistName = [NSString stringWithFormat:@"%@/Names.plist",
                               documentsDirectory];

         NSMutableArray *namesNew  = [[NSMutableArray alloc] initWithContentsOfFile:plistName];

         [namesNew addObject:arrayValues];

         [namesNew writeToFile:plistName atomically:YES];

         return nil;
    }
于 2013-05-13T16:26:04.157 回答
0

您不能将您的 plist 写入您需要使用的包中, NSDocumentDirectory 或者NSCachesDirectory

只需复制您的 plist 以捆绑覆盖它。

NSCachesDirectory注意:了解和https://developer.apple.com/icloud/documentation/data-storage/之间的区别NSDocumentDirectory

将您的 plist 从包复制到文档(在下面的代码缓存中),如果您的包中的 plist 只需要一次,我更喜欢在 appdelegate.m 中使用此代码时- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Names.plist"];
    NSString *plistInDocuments=@"Names.plist";

    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:plistInDocuments];

    NSError *error = nil;
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
        [[NSFileManager defaultManager] copyItemAtPath:sourcePath
                                                toPath:dataPath
                                                 error:&error];
    }
    NSLog(@"Error description-%@ \n", [error localizedDescription]);
    NSLog(@"Error reason-%@", [error localizedFailureReason]);

获取您的文件并覆盖它

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *plistInDocuments=@"Names.plist";
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:plistInDocuments];

        //add object here
        NSMutableArray *namesNew = [[NSMutableArray alloc] initWithContentsOfFile:dataPath];
[namesNew addObject:arrayValues];

        NSError *error = nil;
        if ([myFile writeToFile:dataPath options:NSDataWritingAtomic error:&error]) {
            // file saved
        } else {
            // error writing file
            NSLog(@"Unable to write plist to %@. Error: %@", dataPath, error);
        }
于 2013-05-13T16:37:02.637 回答
0

plist 应该是作为基础对象的字典而不是数组。

NSMutableDictionary *namesNew = [NSMutableDictionary dictionaryWithContentsOfFile:plistpath];
[namesNew setObject: arrayValues forKey: @"Root"];
[namesNew writeToFile:plistpath atomically:YES];
于 2013-05-13T16:27:53.710 回答