开发者

NSMutableDictionary Set data in Plist with key

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 th开发者_C百科e wrong can anyone help ?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜