开发者

objective-c none nil array comes up as nil in if/else statement

开发者 https://www.devze.com 2023-04-08 02:35 出处:网络
The following code is not working as expected.I am setting an array after creating a view but before displaying.I used NSLog to test that the array is set but the if/else sees the array as empty.

The following code is not working as expected. I am setting an array after creating a view but before displaying. I used NSLog to test that the array is set but the if/else sees the array as empty.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear开发者_如何学Python:animated];

    NSLog(@"Planlist is nil %d / has %d objects", (planListArr == nil), [planListArr count]);

    if (planListArr == nil || [planListArr count] == 0) { ... }
    else {
        NSLog(@"Planlist is empty");
    }
}  

Logs

2011-09-25 13:54:39.764 myVI[2938:13303] Planlist is nil 0 / has 8 objects
2011-09-25 13:54:39.765 myVI[2938:13303] Planlist is empty

PlanList is defined as

NSArray *planListArr;

@property (nonatomic, retain) NSArray *planListArr;


if (planListArr == nil || [planListArr count] == 0) { ... }
else {
    NSLog(@"Planlist is empty");
}

Expanded, this becomes:

if (planListArr == nil || [planListArr count] == 0) {
    ...
} else {
    NSLog(@"Planlist is empty");
}

So basically, it looks like you have your NSLog() statement in the wrong branch.


(!plainListArray && [plainListArray count]>0) ? NSLog(@"PlainList array has %d items",[plainListArray count] : NSLog(@"Oops! Array is not been initialized or it has no items");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号