Thierry
Thierry

Reputation: 6468

Accessing NSDictionary problem

I'm only new to iPhone development, so my apologies if this is a silly question. I'm building various apps based on a book I'm reading and one of their suggestion was to build a mini web browser. I thought this would be easy, but while most of it is, I'm seriously struggling with the NSDictionary.

I have a UISegmentedControl used to display various bookmarks. The bookmark name that is displayed on the buttons of the UISegmentedControl is going to be my key and the url is the value associated with it.

I first try to declare an NSDictonary as a private (global variable), but since I could not get it to work, I resorted to declare it in my header file as follows:

@property (nonatomic, retain) NSDictionary *bookmarks;

I synthesize it and I initialized it in the viewDidLoad as follows:

- (void)viewDidLoad
{
    bookmarks = [NSDictionary
                 dictionaryWithObjectsAndKeys:@"http://www.microsoft.com",
                                              @"Microsoft",
                                              @"http://www.google.com",
                                              @"Google",
                                              @"http://www.apple.com",
                                              @"Apple",
                                              @"http://msdn.microsoft.com",
                                              @"MSDN", nil];
    [super viewDidLoad];
}

I then associated a control with my segmented control and when the event is triggered and the function is called I've got the following code which is called:

- (IBAction) getShortcut:(id)sender
{
  NSString *shortcut;
  shortcut = [shortcuts titleForSegmentAtIndex:shortcuts.selectedSegmentIndex];
  NSString *url = [bookmarks valueForKey:shortcut];
  //[self navigateTo: url];
  [url release];
}

When a button from the UISegmentedControl is clicked, I extract the value and stored it into shortcut and then I try to use the shortcut variable as a key to extract the associated value from the NSDictionary "bookmarks" but it keeps crashing on NSString *url = [bookmarks valueForKey:shortcut]; and bombs out of my function and displays the usual error EXC_BAD_ACCESS

Any help would be greatly appreciated.

T.

Upvotes: 0

Views: 318

Answers (4)

tomk
tomk

Reputation: 1356

The problem is, that you do not retain "bookmarks" in the viewDidLoad method. There is an naming convention mentioned somewhere in the Apple docs: If an intialisation method starts with "init..." the returned object is retained, if not you have to do it yourself. The "dictionaryWithObjectsAndKeys" returns and object with retain count 0, which means, that after the scope of assignment (your viewDidLoad method) it is immediatly released again.

So just put a

[bookmarks retain];

after your initalisation and you are done. Another solution which does the retaining for you

bookmarks = [[NSDictionary alloc] initWithObjectsAndKeys ...];

And you shouldn't release the url in your action. It gets released, once you release the dictionary

Upvotes: 1

Firoze Lafeer
Firoze Lafeer

Reputation: 17143

That dictionary is autoreleased.

Try this:

self.bookmarks = [NSDictionary dictionaryWithObjectsAndKeys...]

Upvotes: 2

sidyll
sidyll

Reputation: 59297

You have two options. one is to deal with the ivar directly as @Matt S. posted. Note that in this case you need to keep you object with enough retain count. You're using and auto released object and causing the error.

The other option is to use the property you already defined:

self.bookmarks = [[NSDictionary ...]];

And the property retains it.

Upvotes: 2

user756245
user756245

Reputation:

You didn't retain the NSDictionary.

Do:

bookmarks = [[NSDictionary
                 dictionaryWithObjectsAndKeys:@"http://www.microsoft.com",
                                              @"Microsoft", nil] retain];

Upvotes: 1

Related Questions