I am using the google places api to search for nearby restaurants. Each restaurant is displayed in a cell in a UITableView. The return response is in the JSON format. Of the the things the JSON contains is a decimal number that represents the rating of the restaurant. I want to display the rating in the cells. However sometimes there is no rating for the restaurant and the value is (null). So I added a check in the cellForRowAtIndexPath method that checks if the value of the rating is null or not.
if([dict valueForKey:kResponseRating] != [NSNull null])
{
NSNumber *rating = [dict valueForKey:kResponseRating];
[cell displayRating:rating];
}
else
{
NSLog(@"Value of rating is null");
}
When I run the application the tableView still crashes as soon as a null value is returned and the string "Value of rating is null" is NOT printed. So its not going into the else statement even tho the value in the json is null.
Ok so I checked if the return value is of type NSString class and its not. Here is what i did in the same method:
if([[dict objectForKey:kResponseRating] isKindOfClass:[NSString class]])
{
NSLog(@"The return response is of type NSString");
}
And it did not go into if statement.
Here is the method that calculates the rating and posts it. The method takes the rating rounds it to the nearest .5 and then displays it as stars out of 5.
-(void)displayRating:(NSNumber*)rating{
double rate = [rating doubleValue];
rate = round(rate * 2.0) / 2.0;
int counter = 0;
double compareVar = 1.0;
while(counter <= 4){
if(rate == 0.0)
{
imgRating[counter] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"starempty.png"]];
[self.contentView addSubview:imgRating[counter]];
}
else
{
if(compareVar < rate)
{
imgRating[counter] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"starfill.png"]];
[self.contentView addSubview:imgRating[counter]];
}
else if(compareVar == rate)
{
imgRating[counter] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"starfill.png"]];
[self.contentView addSubview:imgRating[counter]];
}
else
{
if(compareVar - rate == 0.5)
{
imgRating[counter] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"starhalffill.png"]];
[self.contentView addSubview:imgRating[counter]];
}
else
{
imgRating[counter] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"starempty.png"]];
[self.contentView addSubview:imgRating[counter]];
}
}
counter++;
compareVar = compareVar + 1.0;
}
}
NSLog(@"This is rate: %f", rate);
}
Is a line of code in this method causing a crash?