将数据添加到 plist

发布于 2024-10-31 22:12:25 字数 2166 浏览 0 评论 0原文

我想在代码中创建一个 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];
}

有什么建议吗?谢谢。

I want to create a plist in code and add more "records" from user input to it. I have created a plist, and I can write the user input, but I can't add data to my plist. Every time, it saves the last input and keeps only one "record". This is the code:

-(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];
}

Any suggestions? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2024-11-07 22:12:25

您没有将文件内容传递给 createFileAtPath:contents:attributes:

[[NSFileManager defaultManager]createFileAtPath:filepath contents:nil attributes:nil];

You are not passing the file contents to createFileAtPath:contents:attributes::

[[NSFileManager defaultManager]createFileAtPath:filepath contents:nil attributes:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文