androniennn
androniennn

Reputation: 3137

Instance variable always returning zero

I have an instance variable array:

       @interface ChannelsVC ()
        {
        NSArray *arrayofFrequencies;
        }
    }
    @end

@implementation ChannelsVC
......

made its allocation in the ´viewDidLoad´

arrayofFrequencies=[[NSArray alloc]init];

Then, I'm inserting data in the array from a JSON data source:

-(void)GetFrequencies{

    NSString *urla=@"http://xxxxxxxx";
    NSString *uria = [urla stringByAppendingString:self.lien_satellite];
    NSURL *urlFrequencies= [ NSURL URLWithString:uria];

    NSLog(@"Frequencies URI: %@",uria); //correct URI



    NSURLRequest *request = [NSURLRequest requestWithURL:urlFrequencies];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        arrayofFrequencies=[JSON valueForKeyPath:@"frequency"];

        NSLog(@"Frequencies count: %lu", (unsigned long)[arrayofFrequencies count]);//the count here is correct, I have data in the array

     } failure:nil];

    [operation start];

 }

The problem is when wanting to make a UITableView that its number of sections is ´[arrayofFrequencies count];´ , it's always returning ´0´ ! It's an instance variable and it's returning zero outside the ´GetFrequencies´ function.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"number of sections %ld",(long)[arrayofFrequencies count]); // always returns 0
    return [arrayofFrequencies count];
}

Am I doing something wrong?

Thank your for helping.

Console output:

013-06-25 12:58:00.163 Frequencies[840:c07] number of sections 0
2013-06-25 12:58:00.165 Frequencies[840:c07] number of sections 0
2013-06-25 12:58:00.641 Frequencies[840:c07] Frequencies count: 4

Upvotes: 0

Views: 69

Answers (3)

Gyanendra
Gyanendra

Reputation: 361

Possible guess for this is tableView delegate is getting called earlier but the response from server is coming after some time so after getting the response try to reload the tabview below is the code.

    -(void)GetFrequencies{

        NSString *urla=@"http://xxxxxxxx";
        NSString *uria = [urla stringByAppendingString:self.lien_satellite];
        NSURL *urlFrequencies= [ NSURL URLWithString:uria];
        NSLog(@"Frequencies URI: %@",uria); //correct URI
        NSURLRequest *request = [NSURLRequest requestWithURL:urlFrequencies];
        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

            arrayofFrequencies=[JSON valueForKeyPath:@"frequency"];
            NSLog(@"Frequencies count: %lu", (unsigned long)[arrayofFrequencies count]); 
            // reload the table here 
            [tableView reloadData];
         } failure:nil];
        [operation start];
    }

Try doing this.

Upvotes: 0

CRDave
CRDave

Reputation: 9285

Your Log Consol show it very clear that your datasource method called before your data load.

Which is very normal because you have setup datasource in storyboard(or interface) or in viewDidLoad.

You just have to reload your UITableView after dataload

Try this:

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        arrayofFrequencies=[JSON valueForKeyPath:@"frequency"];

        [tableView setDataSource:self];
        [tableView reloadData];

     } failure:nil];

You should remove Datasource from interface builder or storyboard or viewDidLoad so UITableView dont reload it self twice.

Upvotes: 1

Durgaprasad
Durgaprasad

Reputation: 1951

Call [tableView reloadData] after

NSLog(@"Frequencies count: %lu", (unsigned long)[arrayofFrequencies count]);//the count here is correct, I have data in the array

Point to understand is in viewDidLoad tableView delegates are called. At that time array count is zero. Later array is filled with object. reloadData method call delegate method of table again.

Upvotes: 2

Related Questions