简体   繁体   English

以编程方式添加新项目以列表

[英]Adding new items to plist programmatically

In my last question I asked for advice on saving/loading from file. 最后一个问题中,我要求提供有关从文件保存/加载的建议。 Plist advice was pretty awesome, but of course I had to run into some kind of problem. Plist的建议非常棒,但是我当然要遇到某种问题。 I already populated my TableView>DetailView controllers with plist. 我已经用plist填充了TableView> DetailView控制器。 Now I have to somehow save my data(from parse.com). 现在,我必须以某种方式保存我的数据(来自parse.com)。 Here is my current plist, I filled it manually and for sake of simplicity(please don't kill me) I made all items strings: 这是我当前的plist,我手动填充了它,为了简单起见(请不要杀死我),我将所有项目都设为字符串:

<plist version="1.0">
<dict>
    <key>orderID</key>
    <array>
        <string>idKMVX956</string>
        <string>idEWUQ321</string>
    </array>
    <key>orderdate</key>
    <array>
        <string>28.06.2015</string>
        <string>26.06.2015</string>
    </array>
    <key>match</key>
    <array>
        <string>Team 3 vs Team 4</string>
        <string>Team 1 vs Team 2</string>
    </array>
    <key>date</key>
    <array>
        <string>20.06.2015</string>
        <string>20.06.2015</string>
    </array>
    <key>sector</key>
    <array>
        <string>2</string>
        <string>1</string>
    </array>
    <key>seat</key>
    <array>
        <string>R2S75</string>
        <string>R1S1</string>
    </array>
    <key>price</key>
    <array>
        <string>120</string>
        <string>60</string>
    </array>
</dict>
</plist>

Now my question is - how to add new order to this plist. 现在我的问题是-如何向此plist添加新订单。 Just for sake of testing I wrote this method: 为了测试,我编写了以下方法:

- (void)saveToPlist
{
    NSString *matchname = @"Team 5 vs Team 6";
    NSString *orderHistoryFile = [[NSBundle mainBundle] pathForResource:@"OrderHistory" ofType:@"plist"];
    orders = [[NSMutableDictionary alloc] initWithContentsOfFile:orderHistoryFile];
    match = [orders objectForKey:@"match"];
    NSLog(@"matches: %@", match);
    [match addObject:matchname];
    NSLog(@"addedTeam; %@", match);
    [match writeToFile:orderHistoryFile atomically:YES];
}

But while second NSLog shows that indeed line "Team 5 vs Team 6" was added to array - no changes appear in plist file and do not appear on tableview. 但是,尽管第二个NSLog显示确实将“ Team 5 vs Team 6”行添加到了阵列-plist文件中没有出现更改,并且没有出现在tableview中。 Why? 为什么?

@Update alright I see where my mistake was with writetofile. @Update好吧,我知道我的错误在哪里与writetofile在一起。 However now after executing this method - it works once and I can see details. 但是现在执行此方法后-它可以工作一次,我可以看到详细信息。 Then I can't load data anymore and table does not populate. 然后,我无法再加载数据,并且表格也不会填充。 Why? 为什么? Update2 So i can't write in my bundle directory(ty for advice). Update2所以我不能在我的捆绑包目录中写(建议)。 That made me try to move whole process to documents. 这使我试图将整个过程转移到文档上。 Here is some code i wrote: 这是我写的一些代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *filePath = [[self fileDirectory] stringByAppendingString:@"OrderHistory.plist"];
    if (![[NSFileManager defaultManager]fileExistsAtPath:filePath]){
     [[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle]pathForResource:@"OrderHistory" ofType:@"plist"] toPath:filePath error:nil]; 
     }
    orders = [[NSDictionary alloc] initWithContentsOfFile:filePath];
    orderId = [orders objectForKey:@"orderID"];
    NSLog(@"count orders: %d", [orderId count]);
}
- (NSString *)fileDirectory{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

}

Now my NSLog counting items outputs 0. Everything works fine with bundle directory, so I suspect that file is not even created in documents. 现在我的NSLog计数项目输出为0。一切都可以在bundle目录中正常工作,因此我怀疑文件甚至没有在文档中创建。 Where is my mistake? 我的错误在哪里?

您无法写入应用程序捆绑包,因此需要将文件保存到Documents文件夹或等效Documents夹(例如Caches文件夹)中。

Your mistake is in the line: [[self fileDirectory] stringByAppendingString:@"OrderHistory.plist"]; 您的错误所在的行中: [[self fileDirectory] stringByAppendingString:@"OrderHistory.plist"];

It should be -stringByAppendingPathComponent , when you append it like string you'll miss the / which is relevant for directoryPath. 它应该是-stringByAppendingPathComponent ,当您像字符串一样附加它时,您会错过/ ,它与directoryPath有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM