Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.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
Ios 查询NSDictionary以查找if/else语句中的值_Ios_Objective C_Json_Nsdictionary - Fatal编程技术网

Ios 查询NSDictionary以查找if/else语句中的值

Ios 查询NSDictionary以查找if/else语句中的值,ios,objective-c,json,nsdictionary,Ios,Objective C,Json,Nsdictionary,我的parse cloud代码函数设置为以JSON的形式返回数据,如下所示: response.success ({ "results": [ { "Number of top categories": top2.length }, { "Top categories": top2 }, { "Number of mat

我的parse cloud代码函数设置为以JSON的形式返回数据,如下所示:

response.success
({
                "results": [
                  { "Number of top categories": top2.length },
                            { "Top categories": top2 },  
                         { "Number of matches": userCategoriesMatchingTop2.length }, 
         { "User categories that match search": userCategoriesMatchingTop2 }
                ]
});
我要做的是在我的Objective-C代码中查询这个JSON数组,并通过使用底部的if语句,根据返回的内容执行某些操作。例如,它说:

if ([result intValue] == 1){
                                                    [self performSegueWithIdentifier:@"ShowMatchCenterSegue" sender:self];
                                                }
我想用一条语句替换
result intValue
,该语句表示JSON数据的“匹配数”的值等于1

- (IBAction)nextButton:(id)sender
{
    if (self.itemSearch.text.length > 0) {
        [PFCloud callFunctionInBackground:@"eBayCategorySearch"
                           withParameters:@{@"item": self.itemSearch.text}
                                    block:^(NSString *result, NSError *error) {
                                        NSLog(@"'%@'", result);

                                        NSData *returnedJSONData = result;

                                            NSError *jsonerror = nil;

                                            NSDictionary *categoryData = [NSJSONSerialization
                                                                     JSONObjectWithData:returnedJSONData
                                                                     options:0
                                                                     error:&jsonerror];

                                            if(error) { NSLog(@"JSON was malformed."); }

                                            // validation that it's a dictionary:
                                            if([categoryData isKindOfClass:[NSDictionary class]])
                                            {
                                                NSDictionary *jsonresults = categoryData;
                                                /* proceed with jsonresults */
                                            }
                                            else
                                            {
                                                NSLog(@"JSON dictionary wasn't returned.");
                                            }



                                        if (!error) {

                                            // if 1 match found clear categoryResults and top2 array
                                            if ([result intValue] == 1){
                                                [self performSegueWithIdentifier:@"ShowMatchCenterSegue" sender:self];
                                            }

                                            // if 2 matches found
                                            else if ([result intValue] == 2){
                                                [self performSegueWithIdentifier:@"ShowUserCategoryChooserSegue" sender:self];
                                                //default to selected categories criteria  -> send to matchcenter -> clear categoryResults and top2 array
                                            }

                                            // if no matches found, and 1 top category is returned
                                            else if ([result intValue] == 2) {
                                                [self performSegueWithIdentifier:@"ShowCriteriaSegue" sender:self];
                                            }
                                            // if no matches are found, and 2 top categories are returned
                                            else if ([result intValue] == 2) {
                                                [self performSegueWithIdentifier:@"ShowSearchCategoryChooserSegue" sender:self];
                                            }

                                        }
                                    }];
    }
}

据我所知,您得到的响应
JSON
数据是字典中的一个字典数组

要检索这些值中的每一个,可以使用以下步骤:

第1步:

将结果字典中的字典数组分离到
NSArray
对象中

NSArray *resultArray = [resultDictionary objectForKey:@"results"];
第二步:

现在您有了
resultArray
,您可以按如下方式提取所需的值:
假设您需要
NSNumber
对象
的值“匹配数”
, 您知道它是
resultArray
中的
3rd
对象,因此它的索引是
2

NSDictionary *dictionary = [resultArray objectAtIndex:2];
NSNumber *numberOfMatches = [dictionary objectForKey:@"Number of matches"];
现在,您可以随时随地使用
[numberOfMatches intValue]


希望这有帮助!:)

当使用间接返回
NSError
的方法时,Cocoa要求您在使用
NSError
之前检查直接返回值,而不是检查错误是否为
nil
。如果该方法指示失败,则该错误将有效,但如果成功,则相反的错误将为
nil
,即使您事先将其设置为
nil
,也不能保证该错误有效。您遇到了什么问题?您需要知道结果字典中有多少项?现在还不清楚“匹配数”是什么意思。如果你看看我返回的JSON,
“匹配数”
是其中一个键,它的值是从我的云代码中的
userCategoriesMatchingTop2
数组的长度派生出来的。在这种情况下,如果
userCategoriesMatchingTop2.length
等于1,我想告诉我的iOS应用程序执行某个操作。要实现这一点,您面临的困难是什么?我不确定如何形成执行此操作所需的语法。我已经查阅了NSDictionary文档,不太清楚我将如何处理这种特殊情况。这看起来确实是正确的,而且是有意义的。但是,当我像这样设置代码并运行它时,应用程序崩溃了,我得到了以下结果:请将您编写的用于分隔字典中数据的行移动到
if(!error){}
块。另外,请使用异常断点来获取崩溃的确切位置。从中,我观察到
是“顶级类别”
“匹配搜索的用户类别”
是一个数字数组。因此,您需要将这些字段的类型设置为
NSArray
,而不是
NSNumber