Xcode 将NSMutableArray保存到pList

Xcode 将NSMutableArray保存到pList,xcode,plist,Xcode,Plist,我试图将NSMutableArray存储到pList中,但当我检查data.pList时,里面没有任何值。知道为什么吗 -(void)run { Global *myGlobal = [Global sharedGlobal]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [path

我试图将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");
}
}

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


当保存到pList时,您应该尝试这样做

[totalCategory writeToFile:path atomically:YES]; 这是一个很好的例子

+ (void) saveParams:(int)kotuc one:(NSNumber*)one two:(NSNumber*)two three:(NSNumber*)three four:(NSNumber*)four five:(NSNumber*)five six:(NSNumber*)six seven:(NSNumber*)seven eight:(NSArray*)eight{ 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] retain]; NSArray *objects = [[NSArray arrayWithObjects:one, two, three, four, five, six, seven, eight, nil] retain]; NSMutableDictionary *subDict = [[NSMutableDictionary alloc] initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys]; ////save old [dict setDictionary:dw]; ////save new [dict setObject:subDict forKey:[NSString stringWithFormat:@"%i", kotuc]]; // //write to file [dict writeToFile:path atomically:YES]; [dw release]; [dict release]; [subDict release]; [keys release]; [objects release]; }
如果我不包括[writeToFile]行,那么它将不会将我的字典/数组存储到pList中,不是吗?是的,但您需要将某些内容存入库存,否则writeToFile将不会存储任何内容。问题是即使我能够运行这一行,但没有太多问题,但当我检查我的数据时。pList,其中没有任何值。totalCategory是否包含数据?代码的奇怪之处在于,您希望在循环中拾取数据,但设置了静态键类别或allCategory,任何字典中的键都假定是唯一的。通过这种方式,每个字典只能获得记录,因此不需要循环。是的,totalCategory中有7个不同的类别,我想将其中的7个存储到pList中。有没有任何教程对pList有很好的解释?大多数都有关于如何启动到plist的路径的代码,例如documentDirectory和stuff。我没有看到任何关于plist的教程,但是关于plist的代码看起来不错,除了带有stock的行。如果这样做,则拾取已保存在路径a中的数据,然后将其写回路径,这样它就不会执行任何操作。您需要按照我的建议将totalCategory写入路径。 + (void) saveParams:(int)kotuc one:(NSNumber*)one two:(NSNumber*)two three:(NSNumber*)three four:(NSNumber*)four five:(NSNumber*)five six:(NSNumber*)six seven:(NSNumber*)seven eight:(NSArray*)eight{ 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] retain]; NSArray *objects = [[NSArray arrayWithObjects:one, two, three, four, five, six, seven, eight, nil] retain]; NSMutableDictionary *subDict = [[NSMutableDictionary alloc] initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys]; ////save old [dict setDictionary:dw]; ////save new [dict setObject:subDict forKey:[NSString stringWithFormat:@"%i", kotuc]]; // //write to file [dict writeToFile:path atomically:YES]; [dw release]; [dict release]; [subDict release]; [keys release]; [objects release]; }