NSMutableDictionary 使用 key 在 Plist 中设置数据

发布于 2024-12-01 20:08:07 字数 1394 浏览 0 评论 0原文

我有以下代码,

    for (int j= 0; j<data.count; j++){
        NSString *thedata = [data objectAtIndex:j];
        NSMutableDictionary * booksList = [NSMutableDictionary dictionary];
        if(j==0){
            [booksList setValue:[NSString stringWithFormat:@"%@",thedata]  forKey:@"name"];

        }
        if(j==1){
            [booksList setValue:[NSString stringWithFormat:@"%@",thedata]  forKey:@"author"];

        }

        [plistDict setObject:booksList forKey:@"Categories"];
     [plistDict writeToFile:pListPath atomically: YES];
    }

该代码假设将 booksList 保存到 plist 中称为类别的键中。这里发生的情况是,关键类别被清空,就好像我正在向其写入 null 一样。我已经记录了 booksList它返回了,

> 2011-08-27 14:22:15.821 SparkLibrary[8306:13403] {
    name = "\"dark matter\"";
}
2011-08-27 14:22:15.823 SparkLibrary[8306:13403] {
    author = "\"tamer ibrahim\"";
}
2011-08-27 14:22:15.824 SparkLibrary[8306:13403] {
}
2011-08-27 14:22:15.825 SparkLibrary[8306:13403] {
}
2011-08-27 14:22:15.826 SparkLibrary[8306:13403] {
}
2011-08-27 14:22:16.915 SparkLibrary[8306:13403] {
    name = "\"crypt tales\"";
}
2011-08-27 14:22:16.917 SparkLibrary[8306:13403] {
    author = "\"tamer ibrahim\"";
}
2011-08-27 14:22:16.918 SparkLibrary[8306:13403] {
}
2011-08-27 14:22:16.919 SparkLibrary[8306:13403] {
}
2011-08-27 14:22:16.920 SparkLibrary[8306:13403] {
}

那么有什么错误有人可以帮忙吗?

i have the following code

    for (int j= 0; j<data.count; j++){
        NSString *thedata = [data objectAtIndex:j];
        NSMutableDictionary * booksList = [NSMutableDictionary dictionary];
        if(j==0){
            [booksList setValue:[NSString stringWithFormat:@"%@",thedata]  forKey:@"name"];

        }
        if(j==1){
            [booksList setValue:[NSString stringWithFormat:@"%@",thedata]  forKey:@"author"];

        }

        [plistDict setObject:booksList forKey:@"Categories"];
     [plistDict writeToFile:pListPath atomically: YES];
    }

this code suppose to save the booksList into plist in the key called categories .. what is happen here is that the key categories get emptied as if i'm writing a null to it.. i've logged the booksList and it returned

> 2011-08-27 14:22:15.821 SparkLibrary[8306:13403] {
    name = "\"dark matter\"";
}
2011-08-27 14:22:15.823 SparkLibrary[8306:13403] {
    author = "\"tamer ibrahim\"";
}
2011-08-27 14:22:15.824 SparkLibrary[8306:13403] {
}
2011-08-27 14:22:15.825 SparkLibrary[8306:13403] {
}
2011-08-27 14:22:15.826 SparkLibrary[8306:13403] {
}
2011-08-27 14:22:16.915 SparkLibrary[8306:13403] {
    name = "\"crypt tales\"";
}
2011-08-27 14:22:16.917 SparkLibrary[8306:13403] {
    author = "\"tamer ibrahim\"";
}
2011-08-27 14:22:16.918 SparkLibrary[8306:13403] {
}
2011-08-27 14:22:16.919 SparkLibrary[8306:13403] {
}
2011-08-27 14:22:16.920 SparkLibrary[8306:13403] {
}

so what the wrong can anyone help ?

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

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

发布评论

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

评论(1

风启觞 2024-12-08 20:08:07

您正在为循环的每次迭代重新创建一个空白字典。移至

NSMutableDictionary * booksList = [NSMutableDictionary dictionary];

for 语句开始之前。而且,如果我理解正确,请将 write 和 setobject 移至循环结束之后。

You are recreating a blank dictionary for each iteration of the loop. Move

NSMutableDictionary * booksList = [NSMutableDictionary dictionary];

To before the start of your for statement. And, if I understand correctly, move the write and setobject to after the end of the loop.

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