2013-05-13 110 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); 
     }