views:

25

answers:

1

I have a simple iPhone app that Im learning and I want to have an instance variable called urlLists which is an NSDictionary

I have declared it like so:

@interface MyViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
    IBOutlet UIPickerView *pickerView;
    NSMutableArray *categories;
    NSDictionary *urlLists;
}
@property(retain) NSDictionary *urlLists;
@end

and in the implementation:

@implementation MyViewController
@synthesize urlLists;

...

- (void)viewDidLoad {
    [super viewDidLoad];

    categories = [[NSMutableArray alloc] init];

    [categories addObject:@"Sport"];
    [categories addObject:@"Entertainment"];
    [categories addObject:@"Technology"];
    [categories addObject:@"Political"];

    NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", @"value3", @"value4", nil];
    urlLists = [NSDictionary dictionaryWithObjects:objects forKeys:categories]; 

    for (id key in urlLists) {
        NSLog(@"key: %@, value: %@", key, [urlLists objectForKey:key]);
    }
}

...
@end

And, this all works up to here.

I have added a UIPicker to my app, and when I select one of the items, I want to Log the one picked and its related entry in my dictionary.

-(void) pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger) component {
    for (id key in self.urlLists) {
        NSLog(@"key: %@, value: %@", key, [urlLists objectForKey:key]);
    }
}

but I get the old EXC_BAD_ACCESS error...

I know Im missing something small, but what?

Thanks

EDIT::

Just fixed it, I added a retain after the dictionaryWithObjects

But I dont know why I need to, shouldnt dictionaryWithObjects create an autorelease object?

+3  A: 

The NSDictionary was deallocated.

You should use self.urlLists = ..., so it goes via the setter, which retains it.

Omitting self will set the ivar directly.

Jaka Jančar
bingo! thanks a lot Jaka, missing the `self` :)
Mark