Willem
Willem

Reputation: 1

Instance variable not returning a value in iOS application

This problem might be related to Instance variable not retaining value in iOS application.

My application has a table view that's supposed to be populated with data from a plist. The plist is question is determined based on a defaults setting.

After setting the contents of the instance variable, a count on it shows that it contains multiple records. However, if I then later try to count this instance variable when drawing the table view cells, it shows no records.

--

I declared a "providerData" NSDictionary instance variable in my class header:

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController {

    NSDictionary *providerData;

}

@property (nonatomic, retain) NSDictionary *providerData;

- (void)loadProviderDataSource:(NSString *)providerName;

@end

In my implementation I use viewDidLoad to read a dictionary from a plist file, check the value of "provider_preference", and then call [self loadProviderDataSource:providerPreference]:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Retrieve the user's provider preference from defaults:
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    // Set a default value for provider preference if the user didn't explicitly set one:
    NSString *providerPreference = [[NSString alloc] initWithString:@"foo"];

    if ([defaults stringForKey:@"provider_preference"]) {
        providerPreference = [defaults stringForKey:@"provider_preference"];
    }

    // Load provider data from the relevant plist:
    [self loadProviderDataSource:providerPreference];
}

This all works fine (using NSLog I can see that the plist file is read correctly and that a correct providerPreference value is passed to loadProviderDataSource.

The implementation for loadProviderDataSource is as follows:

- (void)loadProviderDataSource:(NSString *)providerName
{
    // Set the filename and path for the data source:
    NSString *dataSourceFileName = [[NSString alloc] initWithFormat:@"data-%@.plist",providerName];
    NSString *dataSourcePath = [[NSBundle mainBundle] bundlePath];
    dataSourcePath = [dataSourcePath stringByAppendingPathComponent:dataSourceFileName];

    // Read the provider data:
    self.providerData = [[NSDictionary alloc] initWithContentsOfFile:dataSourcePath];

    NSLog(@"provider data entry count 1: %d", [self.providerData count]);
}

At this point the NSLog shows me that there are 2 entries in providerData.

However, when I have to set the cells for my table view, the NSLog in the following code shows me that providerData has 0 entries:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSLog(@"provider data entry count 2: %d", [self.providerData count]);

    return cell;
}

In loadProviderDataSource I've tried using:

self.providerData = [[NSDictionary alloc] initWithContentsOfFile:dataSourcePath];

and:

[self setProviderData:[[NSDictionary alloc] initWithContentsOfFile:dataSourcePath]];

... neither work and I still end up with this in my log:

provider data entry count 1: 2
provider data entry count 2: 0
provider data entry count 2: 0

Any ideas?

Upvotes: 0

Views: 298

Answers (1)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

Based on NSLog(@"%@", self); in both the method which shows up as follows

loadProviderDataSource

self: <ViewController: 0x83739b0> 

tableView:cellForRowAtIndexPath

self: <ViewController: 0x8373170>

It looks like you are creating a second instance of ViewController somehow.

I understand one of them is created using XIB. Verify whether you are creating another one programmatically.

Upvotes: 3

Related Questions