2014-02-12 93 views
-1

嗨,我已經建立了一個表格視圖來顯示在plist entires,並有一個部分添加新的數據到plist,但我無法保存數據...它存儲良好,顯示正常但當我離開應用程序和重新加載新數據已經消失在這裏是如何保存Xcode保存到Plist

- (IBAction) save: (id) sender { 
NSLog(@"%@", @"Save pressed!"); 

if (self.job != nil) { 
    // We're working with an existing drink, so let's remove 
    // it from the array and just recreate it like we would a new 
    // drink... 
    [jobArray_ removeObject:self.job]; 
    self.job = nil; // This will release our reference and set the property to nil 
} 

// Create a new drink dictionary for the new values 
NSMutableDictionary *newDrink = [[NSMutableDictionary alloc] init]; 
[newDrink setValue:self.nameTextField.text forKey:NAME_KEY]; 
[newDrink setValue:self.ingredientsTextView.text forKey:INGREDIENTS_KEY]; 
[newDrink setValue:self.directionsTextView.text forKey:DIRECTIONS_KEY]; 

// Add it to the master drink array and release our reference 
[jobArray_ addObject:newDrink]; 

// Sort the array since we just added a new drink 
NSSortDescriptor *nameSorter = [[NSSortDescriptor alloc] initWithKey:NAME_KEY ascending:YES selector:@selector(caseInsensitiveCompare:)]; 
[jobArray_ sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]]; 

// Pop the modal view and go back to the list 
[self dismissViewControllerAnimated:YES completion:NULL]; 
} 

我是否犯了一個錯誤,它不保存到plist?它加載我的plist中預設的數據,但不能提前新entrys

感謝

回答

1

請保存在plist中的數據是這樣的:

NSMutableDictionary *dict; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentPath = [paths objectAtIndex:0]; 

    NSString *plistPath = [documentPath stringByAppendingPathComponent:@"myplist.plist"]; 

    NSLog(@"File path = %@", plistPath); 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    BOOL success = [fileManager fileExistsAtPath:plistPath]; 

    // This line saves the dictionary to pList 
    [dict writeToFile:plistPath atomically:YES]; 
+0

如何將我加入到我目前的代碼保存三個值 – Azabella

+0

它已經存在於字典中,只需將最後一行添加到它...通過我的代碼中的給定名稱獲取你的plist的路徑,並寫入我的代碼中提到的最後一行 –

+0

它工作嗎? –