0

私が作成したこのplistがあります

ここに画像の説明を入力

私は、この plist を取得してドキュメント ディレクトリにロードするコントローラー クラスのほとんどを作成したので、読み取り/書き込みが可能です。

現在、読み取りは正常に機能しており、以前は書き込みも機能していましたが、最近、オブジェクトの 1 つ (キャッシュ値) をそれに関連する値を持つ Dictionary に変更しました。この plist に書き込もうとすると、アプリがクラッシュします。

これは私が得ているエラーです。

2012-04-05 09:26:18.600 mycodeTest[874:f803] * キャッチされていない例外 'NSInvalidArgumentException' が原因でアプリを終了してい ます。 keys (5)' *** First throw call stack: (0x12cc022 0x1884cd6 0x1248417 0x12685e2 0x19844 0x17e86 0x17669 0x13b67 0xe53a49 0xe51e84 0xe52ea7 0xe51e3f 0xe51fc5 0xd96f5a 0x1d2aa39 0x1df7596 0x1d21120 0x1df7117 0x1d20fbf 0x12a094f 0x1203b43 0x1203424 0x1202d84 0x1202c9b 0x21aa7d8 0x21aa88a 0x450626 0x77ed 0x1e35 0x1) terminate called throwing an exceptionCurrent言語: 自動; 現在、目的の c

これらすべてを念頭に置いて、値を保存する準備ができたときに別のクラスから呼び出されるメソッドを紹介します。

//This method gets called from another class when it has new values that need to be saved
- (void) saveData:(NSString *)methodName protocolSignature:(NSString *)pSignature protocolVersion:(NSNumber *)pVersion requestNumber:(NSNumber *)rNumber dataVersionReturned:(NSNumber *)dvReturned cacheValue:(NSMutableDictionary *)cValue
{
    // 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"];

    // set the variables to the values in the text fields that will be passed into the plist dictionary
    self.protocol = pSignature;
    self.Version = pVersion;
    self.request = rNumber;
    self.dataVersion = dvReturned;

    //if statment for the different types of cacheValues
    if (methodName == @"GetMan")
    {
        //cache value only returns the one cachevalue depending on which method name was used
        [self.cacheValue setValue:cValue forKey:@"Man"]; //do I need to have the other values of cacheValue dictionary in here? if so how do I do that.
        c
    }
    else if (methodName == @"GetMod")
    {
        [self.cacheValue setValue:cValue forKey:@"Mod"];
    }
    else if (methodName == @"GetSubs")
    {
        [self.cacheValue setValue:cValue forKey:@"Subs"];
    }


    // This is where  my app is falling over and giving the error message
    // create dictionary with values in UITextFields
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: protocol, pVersion, rNumber, dvReturned, cacheValue, nil] forKeys:[NSArray arrayWithObjects: @"Signature", @"Version", @"Request", @"Data Version", @"Cache Value", nil]];

    NSString *error = nil;
    // create NSData from dictionary
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    // check is plistData exists
    if(plistData)
    {
        // write plistData to our Data.plist file
        [plistData writeToFile:plistPath atomically:YES];

        NSString *myString = [[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", myString);
    }
    else
    {
        NSLog(@"Error in saveData: %@", error);
//        [error release];
    }
}

辞書に5つの値を適用しているとわかる限り、4つのキーが5と異なるというエラーが表示されている場合、私は少し迷っています。

編集**私の問題をデバッグするときに気付いたもう1つのことは、0個のキーと値のペアが表示されているため、cacheValueディクショナリが適切に設定されていないように見えるという事実でした??? これは正しいですか、間違っていますか? ここに画像の説明を入力

これは、使用時に以下に提案されているように、plist を xcode に記録するとどうなるかです。[NSDictionary dictionaryWithObjectsAndKeys:..etc

Check setup is everything there?
Temp Dic output = {
    Root =     {
        "Cache Value" =         {
            Manu = 0;
            Mod = 0;
            Sub = 0;
        };
        "Data Version returned" = 0;
        "Signature" = null;
        "Version" = 0;
        "Request Number" = 0;
    };

Run Man cache check results
Temp Dic output = {
    "Version returned" = 5;
    "Signature" = Happy;
    "Version" = 1;
    "Request Number" = 4;

ご覧のとおり、リクエストを実行した後、キャッシュ値が完全に欠落しています。

4

2 に答える 2

0

このような状況では、コードを分割します。一時変数を使用して、各部分を別々の行で実行します。

キーと値を一時的な配列に入れます。

すべての値を取得するか、デバッガーでブレークポイントを設定して、すべての値を調べます。Eliは、cacheValueがnilであることはほぼ間違いなく正しいです。arrayWithObjectsメソッドは最初のnilで停止します。

このコード:

NSString *string1 = @"string 1";
NSString *string2 = @"string 2";
NSString *string3 = @"string 3";
NSString *string4 =  nil;
NSString *string5 = @"string 5";


NSArray *anArray = [NSArray arrayWithObjects:
  string1,
  string2,
  string3,
  string4,
  string5,
  nil];

NSLog(@"anArray has %d elements", [anArray count]);

arrayWithObjects行に5つの要素が追加されているように見えても、配列には3つの要素しか表示されません

于 2012-04-05T00:08:03.737 に答える
0

cacheValueクラッシュが発生したときは nilであると推測し、その結果、values配列には 4 つのオブジェクトしかありませんが、keys.

[NSDictionary dictionaryWithObjectsAndKeys:]代わりに使用してみてください。

于 2012-04-04T23:03:18.227 に答える