プロジェクトにリソースファイル(plist)を追加しました。私は現在、それを読み書きするためのある種の「ヘルパー」をコーディングしています。3つのgetメソッドと3つのsetメソッドがあります。最初のオブジェクトは一番上にあるオブジェクトを返し、2番目のオブジェクトは別のディクショナリ内にあるオブジェクトを返し(コードを参照)、3番目のオブジェクトは任意の深さのオブジェクトを返します。ノード名を指定するだけでそこに到達できます。(私を理解していただければ幸いです)
問題はセッターにあります。「サーフェス」オブジェクトを設定することは大したことではないので、別の辞書にあるオブジェクトを設定することも重要です。問題は、オブジェクトを深さに設定しようとすると発生します。私が何かを書く前に、私が言っていることをあなたが理解できるようにコードを投稿します。
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時間近く起きていて、これを解決する方法を考えることができません。
前もって感謝します。