Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 对象计数(0)与关键点计数(1)不同_Objective C_Object_Key_Plist_Nsdictionary - Fatal编程技术网

Objective c 对象计数(0)与关键点计数(1)不同

Objective c 对象计数(0)与关键点计数(1)不同,objective-c,object,key,plist,nsdictionary,Objective C,Object,Key,Plist,Nsdictionary,一个错误不断出现,说有一个不同的关键点,然后对象计数,程序不断崩溃 下面的代码就是引起质疑的代码 -(void)saveData //error is in here { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //get documents path NSString *documentsPath = [paths objectAtInd

一个错误不断出现,说有一个不同的关键点,然后对象计数,程序不断崩溃

下面的代码就是引起质疑的代码

-(void)saveData //error is in here
{

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:@"Data.plist"];

//set the variables to the values in the text fields
self.topScores = highScore.text;

//create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: topScores, nil] forKeys:[NSArray arrayWithObjects: @"bestScore", nil]];

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

//check if plist data exists
if (plistData)
{
    //write plistData to our Data.plist file
    [plistData writeToFile:plistPath atomically:YES];
}
else
{
    NSLog(@"Error in saveData: %@", error);
    [error release];
 }
}
代码可以工作,但是在这里崩溃了

         if (newTopScore > [topScores intValue]) 
        {
            topScores = ([NSString stringWithFormat:@"%i", newTopScore]); 
        }

        highScore.alpha = 1; 

    [self saveData];
必须是这一行:

NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: topScores, nil] forKeys:[NSArray arrayWithObjects: @"bestScore", nil]];
而且必须是
topscore
为nil,这意味着对象数组的大小为0,而键数组的大小为1

您需要找出为什么
topScores
为零

例如,尝试运行以下代码:

NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:@[] 
                                                      forKeys:@[@"bestScore"]];
您将获得:

***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'***-[NSDictionary initWithObjects:forKeys::]:对象计数(0)不同于键计数(1)


错误读取-[NSDictionary initWithObjects:forKeys:]:对象计数(0)与键计数(1)不同“,这意味着您的plist已损坏。可能有一个数组元素在那里的某个地方。你介意指定数组元素是什么意思吗?因为我看不到任何数组元素,因为我不能告诉你具体是哪一行引起的错误?如果您无法从堆栈跟踪中找出它,您可以单步查看它失败的地方。错误是我在上面列出的错误,但由于某种原因,如果(newTopScore>[topScores intValue]){topscore=([NSString stringWithFormat:@“%I”,newTopScore]);},代码在此崩溃参考估计在这种情况下会引发一个
NSIllegalArgumentException
:@特洛伊木马是的,没错(尽管实际上是一个
NSInvalidArgumentException
)。这就是OP的错误来源。请参阅我的编辑。