我有这个已创建的plist
我编写了大部分控制器类,它获取了这个plist并将其加载到文档目录中,因此可以读取/写入。
目前我的阅读工作正常,而且我曾经也在写作,但是我刚刚将其中一个对象(缓存值)更改为具有与之相关的值的Dictionary。现在,当我尝试写这个plist时,我的应用程序崩溃了。
这是我得到的错误。
2012-04-05 09:26:18.600 mycodeTest [874:f803] *由于终止应用 未捕获的异常'NSInvalidArgumentException',原因:'* - [NSDictionary initWithObjects:forKeys:]:对象计数(4)与键计数(5)不同 ***首先抛出调用堆栈:(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)终止调用throw exceptionCurrent语言:auto;目前的目标-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键值对???这是对还是错?
当我使用[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;
正如您所见,在运行请求后,Cache Value完全丢失。
答案 0 :(得分:0)
我猜测发生崩溃时cacheValue
为零,导致values
数组中只有4个对象,keys
中只有5个。
请尝试使用[NSDictionary dictionaryWithObjectsAndKeys:]
。
答案 1 :(得分:0)
在这种情况下,分解您的代码。使用临时变量将每个部分放在一个单独的行上。
将您的密钥和值放入临时数组中。
批量处理所有内容的值,或在调试器中设置断点并检查所有值。 Eli几乎肯定是cacheValue为零。 arrayWithObjects方法在第一个零停止。
此代码:
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个元素