0

我有一个资源文件(plist)添加到我的项目中。我目前正在编写某种“助手”来读写它。我有 3 个 get 和 3 个 set 方法。第一个返回顶部的对象,第二个返回另一个字典中的对象(参见代码),第三个返回任何给定深度的对象我只需要指定节点名称就可以到达那里。(希望你能理解我)

问题来自于二传手。设置“表面”对象没什么大不了的,设置另一个字典中的对象也是如此。当我尝试将对象设置在某个深度时,问题就来了。在我写任何其他内容之前,我将发布代码,以便您理解我在说什么。

fullContent 是一个NSMutableDictionary包含文件。

//This one is easy, just return the object for the key.
- (id)getSurfaceObjectForKey:(NSString*)key
{
    return [fullContent objectForKey:key];
}

//Hope you understand this one. Main parent is a string with the name of the first node. It gets a dictionary out of my plist and returns an object for key (I have a dictionary structured plist)
- (id)getMainParentChildObjectForKey:(NSString*)key
{
    NSAssert(!mainParent, @"Main parent must not be nil");

    return [[fullContent objectForKey:mainParent] objectForKey:key];
}

//This one gets the element at any given depth I just have to pass in an array containing node names
- (id)getObjectForKey:(NSString *)key atDepthWithChildren:(NSArray *)children
{
    id depthElement = fullContent;

    for (int i = 0; i < children.count; i++)
        depthElement = [depthElement objectForKey:[children objectAtIndex:i]];

    return [depthElement objectForKey:key];
}

//Sets a top (surface) object
- (void)setSurfaceObject:(id)object ForKey:(NSString *)key
{
    [fullContent setObject:object forKey:key];
    [self writePlistContent];
}

//Sets an object inside a dictionary (mainParent - string with the name of dictionary node)
- (void)setMainParentChildObject:(id)object forKey:(NSString *)key
{
    [[fullContent objectForKey:mainParent] setObject:object forKey:key];
    [self writePlistContent]; //Self explanatory. I write this to file
}

//This is where my problem comes. How do I save this to plist without making any other changes to it? Im guessing I have to rebuild it from inside up?
- (void)setObject:(id)object forKey:(NSString *)key atDepthWithChildren:(NSArray *)children
{
    id depthElement = fullContent;

    for (int i = 0; i < children.count; i++)
        depthElement = [depthElement objectForKey:[children objectAtIndex:i]];

    [depthElement setObject:object forKey:key]; //I set the desired object but I dont know how to save it

    for (int i = 0; i < children.count - 1; i++)
    {
        //Here i guess i would have to build the NSDictionary from inside out. Using a NSMutable array perhaps?
    }
}

我希望你能理解我的问题。我希望我不要把事情复杂化太多。我真的很累,现在已经将近 24 小时了,想不出解决这个问题的方法。

先感谢您。

4

1 回答 1

1

我不明白你为什么不只使用你的:

[self writePlistContent];

保存它。

当然,它会保存 plist 的全部内容。

于 2012-10-23T21:01:30.537 回答