2013-10-18 73 views
0

我試圖閱讀很多問題/教程,但我找不到答案! 我試圖從一個文本框得到一個字符串,並將其保存在我創建了一個datas.plist文件(默認鍵根目錄下) 我試過這段代碼:添加一個NSString到.plist(屬性列表)文件

//Save Name to plist 
     if([name.text length] > 0){ 
      NSString *string = name.text; 
      [array addObject:string]; 
      //plist path 
      NSString *path = [[NSBundle mainBundle] pathForResource:@"datas" ofType:@"plist"]; 
      //save object 
      [array writeToFile: path atomically:YES]; 

     } 

其中陣列是

@interface DetailViewController(){ 
    NSMutableArray* array; 
} 

但是當我嘗試應用程序崩潰,以節省給這個錯誤(我連接到一個按鈕之前寫的動作):

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'

我認爲WR代碼它是不正確的,因爲我從來沒有鍵入鑰匙保存(如@「根」)或類似的東西。 在此先感謝

編輯:

我想這代碼,但不寫什麼:

- (void)addToMyPlist { 
    NSString *destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
    destPath = [destPath stringByAppendingPathComponent:@"dati"]; 

    // If the file doesn't exist in the Documents Folder, copy it. 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if (![fileManager fileExistsAtPath:destPath]) { 
     NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"dati" ofType:@"plist"]; 
     [fileManager copyItemAtPath:sourcePath toPath:destPath error:nil]; 
    } 

    // Load the Property List. 
    array = [[NSMutableArray alloc] initWithContentsOfFile:destPath]; 

     //NSString *path = [[NSBundle mainBundle] pathForResource:@"dati" ofType:@"plist"]; 
     NSString *string = self.name.text; 

     NSArray *values = [[NSArray alloc] initWithObjects:string, nil]; 
     NSArray *keys = [[NSArray alloc] initWithObjects:@"Root", nil]; 
     NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys]; 
     [array addObject:dict]; 
     [array writeToFile:destPath atomically:YES]; 
} 

有什麼不對?

+3

如果我每天都這樣被要求對堆棧溢出... – 2013-10-18 11:15:09

+3

你的plist中的問題和您的例外(核心數據)時拿到一分,似乎是無關... – Wain

+0

@Bigood是啊,我做了謝謝 – r4id4

回答

1

包中的所有文件都是隻讀的。

您必須將您的pList移動​​到您的應用程序的私有文檔(或其他文件,其中有3個可用)文件夾才具有寫入權限。

例如,複製文件目錄中的包文件:

if([name.text length] > 0){ 
     NSString *string = name.text; 
     [array addObject:string]; 

     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     NSError *error; 
     //Path to document directory 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 

     //Path to your datas.plist inside documents directory 
     NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:@"datas.plist"]; 

     //If the file doesn't exists yet 
     if ([fileManager fileExistsAtPath:documentsPath] == NO) { 
      //Let's copy it in your Documents folder 
      NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"datas" ofType:@"plist"]; 

      [fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&error]; 
     } 

     [array writeToFile: documentsPath atomically:YES]; 

    } 
+0

我創建了從Xcode新文件>的plist文件...它創建在我的應用程序的文件夾默認情況下,我認爲 – r4id4

+0

我試圖更換你的代碼,但它總是給我這個錯誤: ***由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'無法創建NSPersistentStoreCoord inator與無模型' – r4id4

+1

@ObiWanKeNerd此錯誤與CoreData鏈接。如果你不使用它,請嘗試禁用它,或者刪除AppDelegate中的Coredata init函數 – Bigood