【发布时间】:2011-04-13 17:06:17
【问题描述】:
我想在代码中创建一个 plist,并从用户输入中添加更多“记录”。我创建了一个 plist,我可以编写用户输入,但我无法将数据添加到我的 plist。每次,它都会保存最后一次输入并只保留一个“记录”。这是代码:
-(void) createPlistFile {
manager = [NSFileManager defaultManager];
filepath = @"/Users/agnostos_el/Downloads/PhoneBook/PhoneBook/Epafes.txt";
if ( [manager fileExistsAtPath:filepath] == NO )
{
NSLog(@"Το Αρχειο Δεν Υπαρχει Το δημειουργω");
[[NSFileManager defaultManager]createFileAtPath:filepath contents:nil attributes:nil];
}
}
-(void) openPlistFile {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if( [path count] > 0 ){
pListFile = [[path objectAtIndex:0] stringByAppendingPathComponent:filepath];
NSDictionary *pListDict = [[NSDictionary alloc]initWithContentsOfFile:filepath];
if ( [pListDict count] <= 0 )
{
}
[self createPlistFile];
}
}
- (void) times
{
filepath = @"/Users/agnostos_el/Downloads/PhoneBook/PhoneBook/Epafes.txt";
stoixeia = [NSArray arrayWithObjects:onoma.stringValue, poli.stringValue, odos.stringValue, per.stringValue, tk.stringValue, xora.stringValue,kin.stringValue, kin1.stringValue, kin2.stringValue, kat.stringValue, erg.stringValue, fax.stringValue, email.stringValue, email1.stringValue, email2.stringValue, pros.stringValue, sim.stringValue, nil];
eponimos = epitheto.stringValue;
NSMutableDictionary *atomo = [NSMutableDictionary dictionary];
innerDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: eponimos, stoixeia, nil]
forKeys:[NSArray arrayWithObjects:@"eponimo", @"stoixeia", nil]];
[atomo setObject:innerDict forKey:eponimos];
id plist = [NSPropertyListSerialization dataFromPropertyList:(id)atomo
format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
record = [[NSData alloc]initWithData:plist];
[record writeToFile:filepath atomically:YES];
}
- (void) eggrafi
{
[self openPlistFile];
[self times];
}
有什么建议吗?谢谢。
【问题讨论】:
标签: objective-c cocoa macos plist