2010-08-08 123 views
0

我試圖將數據保存到每個人的應用程序中的plist,但它將其保存到每個人,所以例如,如果我有一個眼睛的顏色領域,我在textfield for John Doe,它爲其他人保存了Brown,有沒有辦法在應用程序中保存這些信息,而不是使用plist?我有沒有運氣嘗試:將數據保存到每個對象或個人的plist

-(NSString *)pathofFile{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docsfolder = [paths objectAtIndex:0]; 
    return [docsfolder stringByAppendingFormat:@"data.plist"]; 
} 

這裏是我在我的觀點做負載代碼:

- (void)viewDidLoad { 
    [self loadPerson]; 
    [super viewDidLoad]; 

    NSString *filepath = [self pathofFile]; 
    if ([[NSFileManager defaultManager]fileExistsAtPath:filepath]) { 
     NSArray *array = [[NSArray alloc]initWithContentsOfFile:filepath]; 
     Drinfo.text = [array objectAtIndex:0]; 
     [array release]; 
    } 

    UIApplication *app = [UIApplication sharedApplication]; 
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillTerminate:) name: UIApplicationWillTerminateNotification object:app]; 

這裏是我爲我的saveperson方法的一部分:

NSMutableArray *array = [[NSMutableArray alloc]init]; 
[array addObject:self.Drinfo.text]; 
[array writeToFile:[self pathofFile] atomically:YES]; 
[array release]; 
+0

一些代碼將是很有益的。這聽起來像是代碼中存在一個相當基本的錯誤,它會構建數據結構以進入plist,如果我們能夠看到它,我們可以很容易地修復它。 – David 2010-08-08 01:54:54

+0

thx大衛,我發佈的代碼 – adea 2010-08-08 20:15:29

回答

0

您可以使用plist,爲plist中的每個人創建一個NSDictionary,然後將顏色字符串添加到該字符串中。具體的步驟是這樣的

  1. 從文件打開的plist,將其設置爲一個NSDictionary,並確保它的一個mutable copy
  2. 創建一個新的NSMutableDictionary併爲該例添加任何屬性。眼睛的顏色。
  3. 添加一個mutable dictionaryplistdictionary與按鍵的人的名字
  4. 保存plist
相關問題