NSMutableDictionary使用键在Plist中设置数据

时间:2011-08-27 11:30:20

标签: iphone plist nsmutabledictionary

我有以下代码

    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] {
}

那么有什么不对的人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

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

NSMutableDictionary * booksList = [NSMutableDictionary dictionary];

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