NSMutableDictionary 使用 key 在 Plist 中设置数据
我有以下代码,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在为循环的每次迭代重新创建一个空白字典。移至
for 语句开始之前。而且,如果我理解正确,请将 write 和 setobject 移至循环结束之后。
You are recreating a blank dictionary for each iteration of the loop. Move
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.