0

尽管我知道至少有 2 或 3 个具有此名称的主题,但到目前为止我还没有找到正确的答案来解决我的问题:我想按顺序编辑 Plist(由 zwoptex(图像/动画程序)创建)将其中的每个数字除以 2。所以在我的 plist 中,我确实有一些键,例如“spriteOffset”,其中 {{182, 160}, {58,75}} 或 {192, 165} 作为值。这些是NSStrings,我只想修改数字,所以我需要检查是否有“{”或空格等,然后转换数字。问题是我真的不知道该怎么做.....另外,我的 plist 管理似乎遗漏了一些东西。我已经放了一些 NSLogs 来在我的 plist 中显示每个这些字符串,但是....什么都没有显示...所以这是我的代码:

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

    }
}

感谢您的任何回复,祝大家事业/热情好运

4

3 回答 3

0

首先,您应该替换[infoDict objectForKey:@"spriteColorRect"][sprite objectForKey:@"spriteColorRect"],因为精灵可能是包含更多信息的字典。
您看不到任何日志,因为-objectForKey:返回nil的密钥不存在。

要更改值,您可能会尝试从字符串创建 CGPoint 或 CGRect,然后对其进行更改,最后将其转换回字符串。(CGPointFromNSString() and NSStringFromCGPoint)

要保存字典的修改版本,请使用 NSDictionary 的-writeToFile:atomically:.

于 2012-03-17T22:51:58.887 回答
0

您示例不记录任​​何内容的原因很可能是因为您的内部for..in循环可能正在查找错误的字典:外部循环获取一个 dictionary sprite,所以内部循环不应该查看字典中的键吗?

如果您想读入一个属性列表,更改其中的一些值,然后写回相同的属性列表,您可能会发现查看NSPropertyListSerialization该类很有用——它可以让您快速获取可变数组/字典的结构plist 数据,因此您可以迭代它们,但是您想更改其中的值,然后再次将整个事物序列化回数据。(如果您使用dictionaryWithContentsOfFile:,您将获得一个可变字典,但其中的所有容器都是不可变的,因此您必须mutableCopy在迭代期间在整个地方进行并调整内容。)

目前没有时间写更多细节,但如果查找 NSPropertyListSerialization 的文档对您没有帮助,我可能会稍后编辑答案。

于 2012-03-17T22:38:50.013 回答
0

好的,我确实成功了,所以如果有人对此感兴趣,这里是代码:

-(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;
} 
于 2012-03-20T19:08:52.213 回答