objective-ccastingnsstringplistzwoptex

Objective C : Writing into a Plist


Even though i know there are at least 2 or 3 topics with this name, i didnt find a proper answer so far to my problem : I want to edit a Plist (which has been created by zwoptex (image/animations program)) in order to divide every number in it by 2. So in my plist i do have some keys like "spriteOffset" with {{182, 160}, {58,75}} or {192, 165} as value. Those are NSStrings, and i just want to modify the numbers so i need to check if there's a "{" or a space or such, then casting the number. The thing is i don't really know how to do it..... Also, it seems that i'm missing something with my plist management. I've put some NSLogs for displaying every of those strings in my plist, but.... nothing gets displayed... So here is my code :

-(void)DivideValues
{
   for(NSString * plistName in plistSubpathsByName)
{
    NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@.plist",plistName]];

    for(NSDictionary * sprite in [infoDict objectForKey:@"frames"])
    {
        for(NSString * string in [infoDict objectForKey:@"spriteColorRect"])
        {
            NSLog(@"%@",string);
        }
        for(NSString * string in [infoDict objectForKey:@"spriteOffset"])
        {
            NSLog(@"%@",string);
        }
        for(NSString * string in [infoDict objectForKey:@"spriteSize"])
        {
            NSLog(@"%@",string);
        }
        for(NSString * string in [infoDict objectForKey:@"spriteSourceSize"])
        {
            NSLog(@"%@",string);
        }
        for(NSString * string in [infoDict objectForKey:@"textureRect"])
        {
            NSLog(@"%@",string);
        }
    }

    }
}

Thanks for any response, and i wish you all good luck for your career/passion


Solution

  • Ok I did succeed so if anyone is interested here is the code :

    -(void)DivideValues
    {
    for(NSString * xflName in [xflSubpathsByName objectEnumerator]){
    
        NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:[sourceFolder stringByAppendingPathComponent:xflName]];
    
        NSDictionary * dictionary = [infoDict objectForKey:@"frames"];
        NSMutableDictionary * mutabledictionary = [[dictionary mutableCopy] autorelease];
    
        for(NSString * pngFileName in dictionary) {
    
            NSDictionary * sprite = [dictionary objectForKey:pngFileName];
    
            NSLog(pngFileName);
            NSMutableDictionary * mutablesprite = [[sprite mutableCopy] autorelease];
    
            NSString * newstring = [self castSpriteRect:[sprite objectForKey:@"spriteColorRect"]];
            [mutablesprite setObject:newstring forKey:@"spriteColorRect"];
    
            newstring = [self castSprite:[sprite objectForKey:@"spriteOffset"]];
            [mutablesprite setObject:newstring forKey:@"spriteOffset"];
    
            newstring = [self castSprite:[sprite objectForKey:@"spriteSize"]];
            [mutablesprite setObject:newstring forKey:@"spriteSize"];
    
            newstring = [self castSprite:[sprite objectForKey:@"spriteSourceSize"]];
            [mutablesprite setObject:newstring forKey:@"spriteSourceSize"];
    
            newstring = [self castSpriteRect:[sprite objectForKey:@"textureRect"]];
            [mutablesprite setObject:newstring forKey:@"textureRect"];
    
            [mutabledictionary setObject:mutablesprite forKey:pngFileName];
    
        }
    
        [infoDict setObject:mutabledictionary forKey:@"frames"];
                [infoDict writeToFile:[sourceFolder stringByAppendingPathComponent:xflName] atomically:NO];
        }
    
    if(!cancelling)
        ++digestStage;
    else
        digestStage = End;
    }
    
    -(NSString *)castSprite:(id)obj{
    CGPoint point = NSPointFromString((NSString *)obj);
    int i = (int)point.x%2 == 0 ?(int)point.x/2:1+(int)point.x/2;
    int j = (int)point.y%2 == 0 ?(int)point.y/2:1+(int)point.y/2;
    NSString * res = [NSString stringWithFormat:@"{%d, %d}",i,j];
    return res;
    }
    
    -(NSString *)castSpriteRect:(id)obj{
    CGRect point = NSRectFromString((NSString *)obj);
    int i = (int)point.origin.x%2 == 0 ?(int)point.origin.x/2:1+(int)point.origin.x/2;
    int j = (int)point.origin.y%2 == 0 ?(int)point.origin.y/2:1+(int)point.origin.y/2;
    int y = (int)point.size.width%2 == 0 ?(int)point.size.width/2:1+(int)point.size.width/2;
    int x = (int)point.size.height%2 == 0 ?(int)point.size.height/2:1+(int)point.size.height/2;
    NSString * res = [NSString stringWithFormat:@"{{%d, %d}, {%d, %d}}",i,j,y,x];
    return res;
    }