Domanda

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

È stato utile?

Soluzione 3

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;
} 

Altri suggerimenti

First of all, you should replace [infoDict objectForKey:@"spriteColorRect"] with [sprite objectForKey:@"spriteColorRect"], since the sprite is probably the dict containing further information.
You don't see any logs because -objectForKey: returns nil for a key that does not exist.

For changing the values, you might try to create a CGPoint or CGRect from the string, then changing it and finally converting it back to a string. (CGPointFromNSString() and NSStringFromCGPoint)

To save the modified version of your dictionary use NSDictionary's -writeToFile:atomically:.

The reason you example logs nothing is most likely because your inner for..in loops are probably looking in the wrong dictionary: the outer loop gets a dictionary sprite, so shouldn't the inner loops be looking at keys in that dictionary?

If you want to read a property list in, change some values in it, and write the same property list back out, you might find it useful to look at the NSPropertyListSerialization class -- it lets you quickly get a structure of mutable arrays/dictionaries from plist data, so you can iterate into them however you'd like to change values within, then serialize the whole thing back to data again. (If you use dictionaryWithContentsOfFile: you'll get a mutable dictionary, but all the containers within it will be immutable, so you'd have to do mutableCopy and swizzle contents all over the place during your iteration.)

No time to write up more detail at the moment, but I might edit the answer later if looking up the docs for NSPropertyListSerialization doesn't help you.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top