将 NSMutableArrays 保存到 pList

Posted

技术标签:

【中文标题】将 NSMutableArrays 保存到 pList【英文标题】:saving NSMutableArrays to pList 【发布时间】:2011-08-02 03:15:12 【问题描述】:

我正在尝试将 NSMutableArray 存储到 pList 中,但是当我检查我的 data.pList 时,里面没有任何值。知道为什么吗?

-(void)run

Global *myGlobal = [Global sharedGlobal];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

NSMutableDictionary *category = [[NSMutableDictionary alloc]init];
NSMutableDictionary *totalCategory = [[NSMutableDictionary alloc]init];
for (int i = 0 ; i < [myGlobal.categoryArray count];i++)
    Category * c = [categoryArray objectAtIndex:i];

    [category setObject:c.name forKey:@"category"];

    [totalCategory setObject:category forKey:@"allCategory"];

    NSMutableDictionary *stock = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    [stock writeToFile:path atomically:YES];

if( totalCategory==nil )
    NSLog(@"failed to retrieve dictionary from disk");


【问题讨论】:

【参考方案1】:

这两行没有意义。第一行将字典从path 读入stock,第二行将其写回。 stock 没有添加任何内容。

NSMutableDictionary *stock = [NSMutableDictionary dictionaryWithContentsOfFile:path];
[stock writeToFile:path atomically:YES];

【讨论】:

如果我不包含 [writeToFile] 行,那么它不会将我的字典/数组存储到 pList 中,不是吗? 是的,但是您需要在stock 中添加一些内容,否则 writeToFile 将不存储任何内容。【参考方案2】:

当保存到 pList 时,你应该试着把它变成这样。

[totalCategory writeToFile:path atomically:YES];

这是一个如何正常工作的示例

+ (void) saveParams:(int)kotuc 一:(NSNumber*)一二:(NSNumber*)二三:(NSNumber*)三四:(NSNumber*)四五:(NSNumber*)五六:(NSNumber* )六七:(NSNumber*)七八:(NSArray*)八 NSString *path = [self getNSPath]; NSDictionary* dw = [[NSDictionary alloc] initWithContentsOfFile:path]; NSMutableDictionary* dict = [[NSMutableDictionary alloc] init]; NSArray *keys = [[NSArray arrayWithObjects:@"one", @"two", @"three", @"four", @"five", @"six",@"seven", @"eight",nil ] 保持]; NSArray *objects = [[NSArray arrayWithObjects:一、二、三、四、五、六、七、八、无] 保留]; NSMutableDictionary *subDict = [[NSMutableDictionary alloc] initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys]; ////保存旧的 [字典集字典:dw]; ////保存新的 [dict setObject:subDict forKey:[NSString stringWithFormat:@"%i", kotuc]]; // //写入文件 [dict writeToFile:path atomically:YES]; [dw发布]; [字典发布]; [子目录发布]; [按键释放]; [对象释放];

【讨论】:

问题是即使我能够运行这条线而没有太多问题,但是当我检查我的 data.plist 时,里面没有任何值。 totalCategory 是否包含数据?您的代码的奇怪之处在于,您想在循环中选择数据,但您设置了一个静态键(类别或 allCategory),任何字典中的键都假设是唯一的。这样你就可以只记录每个字典,所以不需要循环。 是的,totalCategory 中有 7 个不同的类别,我想将其中的 7 个存储到 pList 中。是否有任何教程对 pList 有很好的解释?他们中的大多数只有关于如何启动 plist 路径的代码,例如 documentDirectory 和东西 我还没有看到任何关于 pList 的教程,但是你的代码在 pList 方面看起来不错,除了 stock 行。如果你这样做,你选择已经保存在路径 a 中的数据,然后你把它写回路径,所以它什么也不做。您需要按照我的建议将 totalCategory 写入路径。

以上是关于将 NSMutableArrays 保存到 pList的主要内容,如果未能解决你的问题,请参考以下文章

将 NSMutableArrays 填充到自定义部分

ply格式文件,用C语言怎么读入,并存储在哪种数据结构中

使用不同的 NSMutableArrays 加载 UITableView

是否可以将带有 NSKeyedArchiver 的 NSMutableArray 保存到核心数据中?

如何创建一个包含许多 NSMutableArrays 的 NSMutableDictionary

点云配准 8-pcd与ply文件转换以及数据保存格式:ascll 和二进制转换