目标C:写入Plist

时间:2012-03-17 18:32:59

标签: objective-c casting nsstring plist zwoptex

即使我知道这个名字至少有2个或3个主题,但到目前为止我还没有找到适合我的问题的答案: 我想编辑一个Plist(由zwoptex(图像/动画程序)创建),以便将其中的每个数字除以2。 所以在我的plist中,我确实有一些像“spriteOffset”这样的键,其中{{182,160},{58,75}}或{192,165}为值。这些是NSStrings,我只想修改数字,所以我需要检查是否有“{”或空格等,然后输出数字。 事情是我真的不知道怎么做..... 而且,似乎我错过了我的plist管理的东西。我已经放了一些NSLog来显示我的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);
        }
    }

    }
}

感谢您的回复,祝您在事业/激情中好运

3 个答案:

答案 0 :(得分:0)

您的示例没有记录任何内容的原因很可能是因为您的内部for..in循环可能正在查找错误的字典:外部循环获取字典sprite,因此内部循环不应该看起来在那个字典中的键?

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

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分: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;
} 
相关问题