0

現在、plist のコントローラー クラスを作成しています。この plist には、いくつかの型 (Number、String、Dictionary) を持つルート ディクショナリがあります。コントローラー クラスでは、plist をチェックしてからドキュメントに追加するので、読み書きできます。

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

これは、plist コントローラー クラスでの read メソッドの外観です。

-(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?
}

データを変数に入れる理由は、後でこれらの値を使用して、データベースに対して実行したいチェックに対してテストするためです..正しいリクエスト番号を受け取っているなどのことを確認します.

更新::ルート辞書内の辞書にそれらを追加するという私の考えは、このようなものになります。私がやろうとしていることへのより良い手がかりを与えるかもしれません。

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

どんな助けでも大歓迎です。

ここに画像の説明を入力

4

1 に答える 1

1

私はあなたが次のことをしたいと信じています:

.h で cacheValue プロパティを可変ディクショナリとして定義します。

NSMutableDictionary *cacheValue;

plistXml を NSMutableDictionary としてシリアル化します。

// 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 でないことを確認してください。キーは、plist に表示されているとおりである必要があります。

Mutable Dictionary の値を更新するのは簡単です。

[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];

これが役に立てば幸いです、乾杯!

于 2012-04-04T02:40:09.867 に答える