2012-04-04 8 views
0

私は現在、このplistのplist.inのコントローラクラスを作成しています。いくつかの型(番号、文字列、辞書)を持つルート辞書があります。私のコントローラクラスでは、plistをチェックしてドキュメントに追加して、読み書きできるようにします。プロパティリストの辞書内の辞書に値を追加する方法

ここから私は現在のplistで何を読み、それらの値をこのクラスに設定したtempvarsに渡します。

これは、私のplistコントローラクラスの読み込み方法のようです。

-(void) readPlistData 
{ 
    // Data.plist code 
    // get paths from root direcory 
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    // get documents path 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    // get the path to our Data/plist file 
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"EngineProperties.plist"]; 

    // check to see if Data.plist exists in documents 
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) 
    { 
     // if not in documents, get property list from main bundle 
     plistPath = [[NSBundle mainBundle] pathForResource:@"EngineProperties" ofType:@"plist"]; 
    } 

    // read property list into memory as an NSData object 
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; 
    NSString *errorDesc = nil; 
    NSPropertyListFormat format; 
    // convert static property liost into dictionary object 
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; 
    if (!temp) 
    { 
     NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); 
    } 
    // assign values 
    self.protocolSignature = [temp objectForKey:@"Protocol"]; 
    self.requestNumber = [temp objectForKey:@"RequestNumber"]; 

    //How do I add the dictionary values here? 
} 

私は変数にデータを入れた理由は、私が正しい要求番号を受け付けておりますようなもののを確認して..私は私のデシベルに対して実行するチェックに対してテストするためにこれらの値を使用するつもりだ後者

UPDATE ::私のアイデアは、ルート辞書内の辞書に追加するのがこのようなものです。私は近いとは思わないが、それはあなたに私がやろうとしていることのより良い手がかりを与えるかもしれない。

self.cacheValue = [temp objectForKey:@"Cache Value"]; 
self.manufacturers = [cacheValue objectForKey:@"Manufacturers"]; 
    self.models = [cacheValue objectForKey:@"Model"]; 
    self.subModels = [cacheValue objectForKey:@"SubModels"]; 

ご協力いただければ幸いです。

enter image description here

+1

plistの基本例はありますか?私はあなたの例でそれを実際に追いかけていません。あなたはNSMutableDictionaryを使いたいと思うのですか? – rwyland

+0

私は上記のように私のplistのスクリーンショットを更新しました。キャッシュ値ディクショナリとそのサブ値をコード化する方法を理解しようとしています。 –

答えて

1

私はあなたが次のことをやりたいと考えている:

可変辞書としての.hであなたのcacheValueプロパティを定義します。

NSMutableDictionary *cacheValue; 

NSMutableDictionaryとしてplistXmlをシリアル:

// This is the root Dictionary 
NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization propertyListWithData:plistXML options:NSPropertyListMutableContainersAndLeaves format:NSPropertyListXMLFormat_v1_0 error:&error]; 

すべてが可変であるので、あなたが今、更新を読んで挿入し、辞書やそのサブコンテンツの任意の部分を削除することができます。例えば、可変辞書「キャッシュバリュー」をつかむことだけである:

self.cacheValue = [temp objectForKey:@"Cache Value"]; 

は、オブジェクトがキーの値がない場合にはnilでないことを確認することを忘れないでください。 plierに表示される鍵はで、正確にはである必要があります。可変辞書に値を更新

は簡単です:

[self.cache setValue:@"New Value" forKey:@"Sub"]; 

そして最後に、バックのplistへのルート変更可能な辞書の変更を保存するには:

/* 
The flag "atomically" specifies whether the file should be written atomically or not. 
If flag is YES, the receiver is written to an auxiliary file, and then the auxiliary file is renamed to path. 
If flag is NO, the dictionary is written directly to path. 
The YES option guarantees that path will not be corrupted even if the system crashes during writing. 
*/ 
[self.temp writeToFile:plistPath atomically:YES]; 

希望これは、歓声を支援します!

+0

ありがとう、あなたのソリューションを今実装しようとしています。どうすればいいのか教えてください:) –

+0

'NSPropertyListMutableContainersAndLeaves'について教えてくれてありがとう。 –

関連する問題