JSON returns null ios 时 Tableview 崩溃
Tableview crashing when JSON returns null ios
我正在使用 google 地点 api 搜索附近的餐馆。每个餐厅都显示在 UITableView 的一个单元格中。 return 响应采用 JSON 格式。在 JSON 包含的内容中,有一个代表餐厅评级的十进制数。我想在单元格中显示评分。但是有时餐厅没有评级,值为 (null)。所以我在 cellForRowAtIndexPath 方法中添加了一个检查,检查评级的值是否为 null。
if([dict valueForKey:kResponseRating] != [NSNull null])
{
NSNumber *rating = [dict valueForKey:kResponseRating];
[cell displayRating:rating];
}
else
{
NSLog(@"Value of rating is null");
}
当我 运行 应用程序时,一旦空值被 returned 且字符串 "Value of rating is null" 未打印,tableView 仍然会崩溃。因此,即使 json 中的值为空,它也不会进入 else 语句。
好的,所以我检查了 return 值是否属于 NSString class 类型,而不是。这是我用同样的方法所做的:
if([[dict objectForKey:kResponseRating] isKindOfClass:[NSString class]])
{
NSLog(@"The return response is of type NSString");
}
并且它没有进入 if 语句。
这是计算评分并发布的方法。该方法将评分四舍五入到最接近的 0.5,然后将其显示为满分 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);
}
这个方法中有一行代码导致崩溃吗?
试试下面的检查
if(![dict objectForKey:kResponseRating] || [dict objectForKey:kResponseRating] == [NSNull null]) {
NSLog(@"Either the key doesn't exist or the value is null");
}
else {
// Covert and set the rating here!
}
这应该可以做到。
if ([[dictionary valueForKey:@"key"] isKindOfClass:[NSNull class]] || ![dict valueForKey:@"key"]){
// Key doesn't exist or equals <null>
}
else{
// Key exist
}
我很确定这个关于堆栈溢出的问题有很多答案。在创建新问题之前,您应该尝试查看您的问题是否有答案。
无论如何,我希望这对您有所帮助
我正在使用 google 地点 api 搜索附近的餐馆。每个餐厅都显示在 UITableView 的一个单元格中。 return 响应采用 JSON 格式。在 JSON 包含的内容中,有一个代表餐厅评级的十进制数。我想在单元格中显示评分。但是有时餐厅没有评级,值为 (null)。所以我在 cellForRowAtIndexPath 方法中添加了一个检查,检查评级的值是否为 null。
if([dict valueForKey:kResponseRating] != [NSNull null])
{
NSNumber *rating = [dict valueForKey:kResponseRating];
[cell displayRating:rating];
}
else
{
NSLog(@"Value of rating is null");
}
当我 运行 应用程序时,一旦空值被 returned 且字符串 "Value of rating is null" 未打印,tableView 仍然会崩溃。因此,即使 json 中的值为空,它也不会进入 else 语句。
好的,所以我检查了 return 值是否属于 NSString class 类型,而不是。这是我用同样的方法所做的:
if([[dict objectForKey:kResponseRating] isKindOfClass:[NSString class]])
{
NSLog(@"The return response is of type NSString");
}
并且它没有进入 if 语句。
这是计算评分并发布的方法。该方法将评分四舍五入到最接近的 0.5,然后将其显示为满分 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);
}
这个方法中有一行代码导致崩溃吗?
试试下面的检查
if(![dict objectForKey:kResponseRating] || [dict objectForKey:kResponseRating] == [NSNull null]) {
NSLog(@"Either the key doesn't exist or the value is null");
}
else {
// Covert and set the rating here!
}
这应该可以做到。
if ([[dictionary valueForKey:@"key"] isKindOfClass:[NSNull class]] || ![dict valueForKey:@"key"]){
// Key doesn't exist or equals <null>
}
else{
// Key exist
}
我很确定这个关于堆栈溢出的问题有很多答案。在创建新问题之前,您应该尝试查看您的问题是否有答案。
无论如何,我希望这对您有所帮助