【问题标题】:saving NSMutableArrays to pList将 NSMutableArrays 保存到 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");
}
}

【问题讨论】:

    标签: xcode plist


    【解决方案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 写入路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2013-03-18
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多