将数组添加到PLIST

时间:2013-05-13 16:23:33

标签: ios objective-c cocoa-touch

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

enter image description here

并没有奏效。这是我的代码:

-(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];
}

我做错了什么?谢谢!

3 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:0)

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

NSMutableDictionary *namesNew = [NSMutableDictionary dictionaryWithContentsOfFile:plistpath];
[namesNew setObject: arrayValues forKey: @"Root"];
[namesNew writeToFile:plistpath atomically:YES];

答案 2 :(得分:0)

您无法将plist写入需要使用NSDocumentDirectoryNSCachesDirectory

的捆绑包

只需复制你的plist就可以覆盖它。

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

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

时在appdelegate.m中使用这段代码
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);
        }