sjors
sjors

Reputation: 117

Can't get objectForKey from NSDictionary

I am using this code: https://github.com/valentinlietz/MySQL-Connect to use MySQL queries in objective c. I use Count(*) to check if this row already exists.

I used this code to NSLog(); this dictionary. Code:

for(NSArray *row in response.responseDictionary){

    NSLog(@"%@", row);

}

Result:

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

But when I use [response.responseDictionary objectForKey:@"COUNT(*)"]; It's returning Unrecognized selector sent to instance.


Update:

Row:

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

Dictionary:

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

Upvotes: 0

Views: 272

Answers (1)

Martin R
Martin R

Reputation: 539745

I haven't worked with that project, but from https://github.com/valentinlietz/MySQL-Connect/blob/master/MySQL.m it looks to me that response.responseDictionary is the result of converting the JSON response of the script https://github.com/valentinlietz/MySQL-Connect/blob/master/mysql.php to a Foundation object.

And the code

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);

in that PHP script shows that the response is an array of dictionaries.

So if I am correct, the name and type of the responseDictionary property is badly chosen, because it is not an NSDictionary, but an NSArray where each object is an NSDictionary, and the following should work:

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

Upvotes: 1

Related Questions