Adding values into my plist

Go To StackoverFlow.com

0

I have this plist that I have created

enter image description here

I have written most of my controller class which gets this plist and loads it into the documents directory so its possible to read/write to is.

Currently I have the reading working fine, and I used to have the writing working also, however I have just recently changed one of the objects (cache value) to a Dictionary with values related to that. Now when I try to write to this plist my app is crashing.

This is the error I am getting.

2012-04-05 09:26:18.600 mycodeTest[874:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSDictionary initWithObjects:forKeys:]: count of objects (4) differs from count of 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 language: auto; currently objective-c

with all of this in mind I will now show you my method, which is called from another class when it has the values ready to be saved.

//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];
    }
}

I am abit lost when the error is saying that 4 keys differ from 5 when as far as i can tell i am applying 5 values to the dictionary any help would be appreciated.

Edit** another thing I noticed when debugging my issues was the fact it looks like I am not getting my cacheValue dictionary set up properly as its showing 0 key valuepairs??? is this right or wrong? enter image description here

this is what happens when I log my plist in xcode as suggested below when I use [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;

as you can see Cache Value is completely missing after I have run the request.

2012-04-04 22:56
by C.Johns


0

I'm going to guess that cacheValue is nil when the crash occurs, resulting in only 4 objects in your values array, but 5 in keys.

Try using [NSDictionary dictionaryWithObjectsAndKeys:] instead.

2012-04-04 23:03
by Ell Neal
I have tried this as per your suggestion, could you look at my updated question above I have added the output... its like I am loosing my cachevalue - C.Johns 2012-04-04 23:18


0

In a situation like this, break up your code. Do each piece on a separate line, with temporary variables.

Put your keys and your values into temporary arrays.

Lot the values of everything, or set breakpoints in the debugger and examine all your values. Eli is almost certainly right that cacheValue is nil. The arrayWithObjects method stops on the first nil.

This code:

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

Will only show 3 elements in the array, even though the arrayWithObjects line appears to add 5 elements

2012-04-05 00:08
by Duncan C
okay thanks.. I think I am getting lost in the code atm - C.Johns 2012-04-05 01:54
Ads