Instance variable not retaining value in iOS appli

2019-02-28 14:33发布

问题:

I have declared this ivar in my

ViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

{   
NSArray *sortedCountries;       
}

@property (nonatomic, retain) NSArray *sortedCountries;

@end

In ViewController.m, sortedCountries does it's job in -(void)ViewDidLoad{} by storing the result of a sorted .plist.

When

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {}

is called below, sortedCountries returns (null)

Why doesn't the value of sortedCountries remain? I added a retain in the first function... I think I am missing a fundamental Objective-C tenant here.

ViewController.m

#import "FirstViewController.h"

@implementation FirstViewController

@synthesize sortedCountries;

-(void)viewDidLoad  {

NSString *path = [[NSBundle mainBundle] pathForResource:@"countries" ofType:@"plist"];  
NSArray *countries = [NSArray arrayWithContentsOfFile:path];
NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease];
NSArray *sortedCountries = [[countries sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]]retain];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {

return 236; 
}

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSDictionary *country = [sortedCountries objectAtIndex:indexPath.row];
NSLog(@"countryDictionary is: %@",country);
NSString *countryName = [country objectForKey:@"name"];
NSLog(@"countryName is : %@", countryName);

    static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier] autorelease];

}

cell.textLabel.text = countryName;
return cell;
} 

回答1:

You are re-declaring sortedCountries as a local variable in viewDidLoad. Use:

sortedCountries = ...

instead (notice no NSArray *). In the code you are using now, sortedCountries would be populated in viewDidLoad, but accessible only in viewDidLoad. You were creating a new variable with the same name instead of setting the class property.



回答2:

NSArray *sortedCountries = [[countries sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]]retain];

to

self.sortedCountries = [countries sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];