-1

I am having this simple code and I really cannot say what is happening:

NSLog(@"Data: %@",[dataFromServer description]);
    for (int i=0; i<[data count]; i++) {
        NSLog(@"Processing data :%d",i);
        NSDictionary *current=[data objectAtIndex:i];
        int category = [[current objectForKey:@"type"] intValue];
        NSLog(@"Category value:%d",category);
        if (category == 1) {
            NSLog(@"Hotel found %@",[current description]);
            [hotelsFromServer addObject:current];
    }
NSLog(@"Hotels. Total:%d Items:%@",[hotelsFromServer count],[hotelsFromServer description]);

I am fetching an NSMutableArray from my server (dataFromServer) which is printed correctly. Each object is an NSDictionary. Category 1 means that the type is a hotel. I enter the if clause, and hotel is being printed correctly.

But outside the for, for the final NSLog, I get these:

Hotels. Total:0 Items:(null)

Why is that? The current item is correct. Why it is not added into my NSMutableArray.

Array is defined like this:

@implementation ClassName
{
   NSMutableArray * dataFromServer;
   NSMutableArray * hotelsFromServer;
}
4

1 回答 1

0

make sure you init the NSMutableArray

self.hotelsFromServer = [[NSMutableArray alloc] init];

in your cause the array is nil i guess and [nil anyMessage] just doesn't do anything

于 2013-10-21T21:16:06.123 回答