Objective c 目标C:写入Plist

Objective c 目标C:写入Plist,objective-c,casting,nsstring,plist,zwoptex,Objective C,Casting,Nsstring,Plist,Zwoptex,尽管我知道至少有2到3个同名主题,但到目前为止,我还没有找到正确的答案: 我想编辑一个Plist(由zwoptex(图像/动画程序)创建),以便将其中的每个数字除以2。 所以在我的plist中,我确实有一些键,比如“spriteOffset”,其值为{182,160}、{58,75}或{192,165}。这些是nsstring,我只想修改数字,所以我需要检查是否有一个“{”或一个空格之类的,然后转换数字。 问题是我真的不知道怎么做。。。。。 另外,我的plist管理似乎缺少了一些东西。我已经在p

尽管我知道至少有2到3个同名主题,但到目前为止,我还没有找到正确的答案: 我想编辑一个Plist(由zwoptex(图像/动画程序)创建),以便将其中的每个数字除以2。 所以在我的plist中,我确实有一些键,比如“spriteOffset”,其值为{182,160}、{58,75}或{192,165}。这些是nsstring,我只想修改数字,所以我需要检查是否有一个“{”或一个空格之类的,然后转换数字。 问题是我真的不知道怎么做。。。。。 另外,我的plist管理似乎缺少了一些东西。我已经在plist中放置了一些nslog来显示这些字符串,但是……没有显示任何内容。。。 这是我的代码:

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

    }
}

感谢您的回复,我祝您的事业/热情好运。您示例中没有记录任何内容的原因很可能是因为您的内部
for..in
循环可能在错误的字典中查找:外部循环得到一个字典
sprite
,所以内部循环不应该在该字典中查找键吗是吗

如果您想读入属性列表,更改其中的一些值,然后写出相同的属性列表,您可能会发现查看
NSPropertyListSerialization
类很有用——它可以让您从plist数据快速获得可变数组/字典的结构,这样您就可以在想要更改值的情况下迭代它们,然后将整个内容重新序列化为数据。(如果您使用
dictionaryWithContentsOfFile:
您将得到一个可变字典,但其中的所有容器都是不可变的,因此您必须在迭代过程中执行
可变复制
并将内容扫遍所有地方。)


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

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

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


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

好的,我成功了,如果有人感兴趣,请看下面的代码:

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

好的,非常感谢。演员阵容看起来很棘手,但是我会的!