2016-09-17 55 views
0
的plist

我試圖將數據添加到plist中,但我不能,讓我們來告訴你,我在做什麼:將數據保存到與iOS

,看一下我的plist:

enter image description here

讓我們看看我的代碼,我創建了2個數組:

@property NSMutableArray *nameArr; 
@property NSMutableArray *countryArr; 

下面是代碼,我保存數據:

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"]; 

[self.nameArr addObject:self.theName.text]; 
[self.countryArr addObject:self.cellPhone.text]; 

NSDictionary *plistDict = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects: self.nameArr, self.countryArr, nil] forKeys:[NSArray arrayWithObjects: @"city", @"state", nil]]; 

NSString *error = nil; 
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; 

if(plistData) 
{ 
    [plistData writeToFile:plistPath atomically:YES]; 
    NSLog(@"Data Saved"); 

} 
else 
{ 
    NSLog(@"Data not saved"); 

} 

下圖顯示錯誤,應用程序終止,但我不知道問題出在哪裏。

enter image description here

+0

你的plist數據設置看起來都是錯誤的。 plist應該包含一個包含多個數組的字典,並且每個數組都應該有相應的一個鍵值。 –

+1

您是從某處複製並粘貼此代碼還是您知道發生了什麼? –

+0

我做了複製並改變了一些,我對plist不太滿意。在同一時間我可以從同一位plist檢索數據在桌面視圖 –

回答

1

大概陣列屬性的聲明,但從未初始化。

你必須在使用前添加

nameArr = [[NSMutableArray alloc] init]; 
countryArr = [[NSMutableArray alloc] init]; 

地方。

關於警告,請使用編譯器建議的方法。