无法从 NSDictionary 获取 objectForKey



我使用这个代码:https://github.com/valentinlietz/MySQL-Connect在objective c中使用MySQL查询。我使用Count(*)来检查这一行是否已经存在。

我使用这段代码NSLog();这本字典。代码:

for(NSArray *row in response.responseDictionary){
    NSLog(@"%@", row);
}
结果:

{
"" = "<null>";
"COUNT(*)" = 0;
}

但是当我使用[response.responseDictionary objectForKey:@"COUNT(*)"];时,它返回发送到实例的未识别选择器。


更新:

行:

Row: ( { "" = "<null>"; "COUNT()" = 0; } )
字典:

Row: { "" = "<null>"; "COUNT()" = 0; }

我还没有与该项目合作,但从https://github.com/valentinlietz/MySQL-Connect/blob/master/MySQL.m看来,我认为response.responseDictionary是结果将脚本https://github.com/valentinlietz/MySQL-Connect/blob/master/mysql.php的JSON响应转换为Foundation对象。

和代码

while($row = mysql_fetch_array($query))
{ 
for($i = 0; $i < 50; $i++) {
     $arr2[$fields[$i]] = $row[$fields[$i]]; 
}
 array_push($return_arr, $arr2);
}
echo json_encode($return_arr);
PHP脚本中的

显示响应是一个字典的数组。

因此,如果我是正确的,responseDictionary属性的名称和类型是错误的选择,因为它不是NSDictionary,而是NSArray,其中每个对象都是NSDictionary,下面的代码应该可以工作:

NSArray *responseArray = (NSArray *)response.responseDictionary;
NSLog(@"count = %@", [[responseArray objectAtIndex:0] objectForKey:@"COUNT(*)"]);

最新更新