开发者

saving NSMutableArrays to pList

i'm trying to store a NSMutableArray into a pList but when i check my data.pList , there isn't any values inside. Any idea why?

-(void)run
{
Global *myGlobal = [Global sharedGlobal];

NSArray *paths = NS开发者_开发百科SearchPathForDirectoriesInDomains(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");
}
}


These two lines don't make sense. The first line reads a dictionary from path into stock, the second line writes it back out. Nothing was added to stock.

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


When saving to the pList, you should try to make it like this.

[totalCategory writeToFile:path atomically:YES];

this is an example how it works fine


+ (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];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜