我有一个资源文件(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小时了,不能想办法解决这个问题。
提前谢谢。
答案 0 :(得分:1)
我不明白为什么你不只是使用你的:
[self writePlistContent];
保存它。
肯定会保存plist的全部内容。