Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing NSDictionary problem
    primarykey
    data
    text
    <p>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 <code>NSDictionary</code>.</p> <p>I have a <code>UISegmentedControl</code> used to display various bookmarks. The bookmark name that is displayed on the buttons of the <code>UISegmentedControl</code> is going to be my key and the url is the value associated with it.</p> <p>I first try to declare an <code>NSDictonary</code> 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:</p> <pre><code>@property (nonatomic, retain) NSDictionary *bookmarks; </code></pre> <p>I synthesize it and I initialized it in the viewDidLoad as follows:</p> <pre><code>- (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]; } </code></pre> <p>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:</p> <pre><code>- (IBAction) getShortcut:(id)sender { NSString *shortcut; shortcut = [shortcuts titleForSegmentAtIndex:shortcuts.selectedSegmentIndex]; NSString *url = [bookmarks valueForKey:shortcut]; //[self navigateTo: url]; [url release]; } </code></pre> <p>When a button from the <code>UISegmentedControl</code> 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 <code>NSDictionary</code> "bookmarks" but <strong>it keeps crashing on</strong> <code>NSString *url = [bookmarks valueForKey:shortcut];</code> and bombs out of my function and displays the usual error EXC_BAD_ACCESS</p> <p>Any help would be greatly appreciated.</p> <p>T.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload