if语句的问题

时间:2011-08-18 05:06:04

标签: objective-c xcode

我正在解析一个json文件。逻辑是这样的,如果名为boy.it的变量中没有数据,则必须进入if循环,如果有数据则进入else部分。但问题是即使变量boy是空的(在控制台中显示为空),循环仍然会进入else部分..你们帮助我们..下面是代码..

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
NSDictionary *boy=[url objectAtIndex:indexPath.row];
NSLog(@"the boy value:%@",boy);
if (boy == NULL) 
{
Secondetailview *detailViewController1 = [[Secondetailview alloc] initWithItem:boy];    
[self.navigationController pushViewController:detailViewController1 animated:YES];
[detailViewController1 release];    
}else
{
    FirstViewDetail *detailViewController = [[FirstViewDetail alloc] initWithItem:boy];

    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}

} enter image description here

3 个答案:

答案 0 :(得分:2)

我会尝试使用if (boy == nil),但这似乎也不起作用。

而是这样做:

NSString *str = [NSString stringWithFormat:@"%@", boy];
if ([str isEqualToString:@""]) {
    //boy is nil do your stuff
}

答案 1 :(得分:1)

替换

if (boy == NULL) 

if ([boy count] == 0) 

答案 2 :(得分:-1)

修改你的if条件如下,看看是否有效。

((boy == NULL) || ([boy count] == 0))