0

我有我创建的这个 plist

在此处输入图像描述

我已经编写了我的大部分控制器类,它获取了这个 plist 并将其加载到文档目录中,因此它可以读取/写入。

目前我的阅读工作正常,而且我以前也有写作工作,但是我最近刚刚将其中一个对象(缓存值)更改为具有相关值的字典。现在,当我尝试写入此 plist 时,我的应用程序崩溃了。

这是我得到的错误。

2012-04-05 09:26:18.600 mycodeTest[874:f803] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSDictionary initWithObjects:forKeys:]:对象数 (4) 与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];
    }
}

当错误说 4 个键与 5 个不同时,我有点迷失了,而据我所知,我正在将 5 个值应用于字典,任何帮助将不胜感激。

编辑**我在调试问题时注意到的另一件事是,我似乎没有正确设置我的 cacheValue 字典,因为它显示了 0 个键值对???这是对还是错? 在此处输入图像描述

这就是当我使用下面的建议在 xcode 中记录我的 plist 时发生的情况[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]);

将仅显示数组中的 3 个元素,即使 arrayWithObjects 行似乎添加了 5 个元素

于 2012-04-05T00:08:03.737 回答
0

我猜cacheValue当崩溃发生时它是 nil ,导致你的values数组中只有 4 个对象,但keys.

尝试[NSDictionary dictionaryWithObjectsAndKeys:]改用。

于 2012-04-04T23:03:18.227 回答