2013-05-13 67 views
0

我想在我的plist中添加和阵列到阵列根:添加阵列到PList

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

我在做什么错?谢谢!

回答

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; 
    } 
+0

没有工作。我了解你的方法,但不知道为什么它不起作用。我记录了'documentsDirectory',它将正确的路径返回到Names.plist,并且文件在那里。所以,我不知道发生了什么。 – 2013-05-13 16:40:06

+0

可以解释多一点..帮助..什么都行不通? – lakesh 2013-05-13 16:42:57

+0

写作。它没写。仍然是同一个文件。 – 2013-05-13 16:44:00

0

的plist这是一个字典作为基础对象,而不是阵列。

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

没有工作。一样。 – 2013-05-13 16:32:47

0

你不能你的plist写信给你需要使用NSDocumentDirectoryNSCachesDirectory

捆只要复制的plist捆扎覆盖它。

注:学习NSCachesDirectoryNSDocumentDirectory https://developer.apple.com/icloud/documentation/data-storage/

之间的区别你的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 *[email protected]"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 *[email protected]"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); 
     }