objective-c none nil array comes up as nil in if/else statement
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");
精彩评论