0

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: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;
1
  • Can you show the code where your defining the array? That last bit of code is known as "declaring" the array. Commented Sep 25, 2011 at 18:02

2 Answers 2

4
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.

Sign up to request clarification or add additional context in comments.

10 Comments

+This is correct. The array is NOT empty or nil, so the else gets evald as true, and nslog prints.
How the NSLog will work for else part "Planlist is empty" if planListArr is nil and its count return 0
@Praveen-K if the array is nil or empty, then "Planlist is empty" won't be logged (in the current code).
sorry, the NSLog should be "Planlist is not empty".
I still can bet, that @DaveDeLong answer is still wrong until he do not change the NSLog value "Plainlist is empty" to "Plainlsit is NOT empty"
|
2
(!plainListArray && [plainListArray count]>0) ? NSLog(@"PlainList array has %d items",[plainListArray count] : NSLog(@"Oops! Array is not been initialized or it has no items");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.