【问题标题】:Navigating plists and settings objects导航 plist 和设置对象
【发布时间】:2012-10-23 20:42:34
【问题描述】:

我的项目中添加了一个资源文件 (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 小时了,想不出办法来解决这个问题。

提前谢谢你。

【问题讨论】:

    标签: iphone objective-c ios plist nsmutabledictionary


    【解决方案1】:

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

    [self writePlistContent];
    

    保存它。

    肯定会保存 plist 的全部内容。

    【讨论】:

    • writePlistContent 用 NSMutableDictionary *fullContent 的内容覆盖整个 plist。
    • 确实如此。但我想用最后一种方法更改 fullContent 中的某些内容,但我不知道如何。看代码。
    • 也许我遗漏了一些东西 - 它发生了 - 但看起来你将 depthElement 设置为 fullContent -> 深入了解完整内容(在 for 循环中) -> 将对象设置为正确NSMutableDictionary -> 现在由您的 fullContent 表示的树应该是正确的,您只需使用 writePlistContent 将整个内容写入闪存。我错过了什么?
    • 我正在将 fullContent 复制到 depthElement,然后在最后用新字典 (depthElement = [depthElement object...]) 覆盖它,然后找到我想要的字典并替换它。所以我在 depthElement 中有一个 NSDictionary,它现在只是解决方案的一部分,对吗?我猜我需要将 depthElement 设置为 fullContent 上一级?
    • 设置好想要的元素后,就不用关心depthElement了。它是您关心的完整内容,它应该是正确的并且可以编写。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    相关资源
    最近更新 更多