1

I have a plist with root of type dictionary containing keys of type array, and each array has three items of type string.

I want to access the strings and pass them to a view controller.

This line is successful in my cellForRowAtIndexPath

NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);

When I put the same line in my didSelectRowAtIndexPath method I get a BAD_ACCESS error.

Any suggestions as to how to get at the strings would be helpful.

Here is my .plist

<dict>
    <key>Derivative</key>
    <array>
        <string>Derivative 1</string>
        <string>front.png</string>
        <string>back.png</string>
    </array>
    <key>Rolle&apos;s Theorem</key>
    <array>
        <string>Rolle&apos;s Theorem 1</string>
        <string>front.png</string>
        <string>3bk.png</string>
    </array>
    <key>Chain Rule</key>
    <array>
        <string>Chain Rule</string>
        <string>chainrule1.png</string>
        <string>chainrule1_bk.png</string>
    </array>
</dict>
</plist>

And here are the two methods:

 - (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];
    }


    NSUInteger row = [indexPath row];

    cell.backgroundColor = [UIColor purpleColor];
    cell.textLabel.textColor = [UIColor blackColor];

    cell.textLabel.text = [self.keys objectAtIndex:row];

    // Key output to NSLOG accessing elements from the plist
    NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);

    cell.textLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:18.0];
    cell.textLabel.numberOfLines = 2;
    cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableCell_BG.png"]] autorelease];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    NSLog(@"Row selected was: %i", row);

    // Key output to NSLOG accessing elements from the plist
    //NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);


/*
    NSString *selectedCard = [[aDictionary objectForKey:(@"%@",[keys objectAtIndex:row])] objectAtIndex:0];
    NSLog(@"Showing Flash Card: %@", selectedCard);
*/

    FlashCardViewController *flashVC = [[FlashCardViewController alloc] initWithNibName:@"FlashCardViewController" bundle:nil];

    id aKey = [keys objectAtIndex:row];

    flashVC.aSelectedCard = [[aDictionary objectForKey:aKey] objectAtIndex:0];

    [self.navigationController pushViewController:flashVC animated:YES];
    [flashVC release];

}
Nungster
  • 726
  • 8
  • 28
  • you are accessing the indexPath's row variable incorrectly. have a look at my posst and it will show you how to get rid of the EXC_BAD_ACCESS error – Pavan Oct 05 '10 at 02:18
  • NSUInteger row = [indexPath row]; that shouldnt work it should be NSUInteger row = indexPath.row; and that works and should get rid of the error. i just tried it myself and i dont get any errors – Pavan Oct 05 '10 at 03:07

2 Answers2

1

My guess is that you are not retaining aDictionary or keys, or you have another memory problem that is manifesting at that point in the code. But it's difficult to tell without the rest of the code.

Alejandro
  • 3,726
  • 1
  • 23
  • 29
  • In my header file I declare the variables like this: – Nungster Oct 05 '10 at 02:56
  • @property (nonatomic, retain) NSDictionary *aDictionary; @property (nonatomic, retain) NSMutableArray *keys; is this sufficient to retain the variables? – Nungster Oct 05 '10 at 02:57
  • Depends on how you construct them: aDictionary = [NSDictionary dictionaryWithContentsOfFile:path] is _not_ enough. – Alejandro Oct 05 '10 at 04:47
  • I thought that since I am declaring it in my .h file with @property (nonatomic, retain) NSDictionary *aDictionary; that that would make it persist. How would you set it so that it does not get released? I truely think that this is why it is behaving like it is. – Nungster Oct 05 '10 at 14:02
  • You either do `self.aDictionary = [NSDictionary dictionaryWithContentsOfFile:path]` or `aDictionary = [[NSDictionary alloc] initWithContentsOfFile:path]` – Alejandro Oct 05 '10 at 16:58
  • Thanks! this did it. I thought that declaring it in the .h and synthesizing the dictionary in the .m was sufficient. Did not realize I still needed to tell it that it was an NSDictionary. – Nungster Oct 05 '10 at 21:43
0

Change this line to

flashVC.aSelectedCard = [[aDictionary objectForKey:[keys objectAtIndex:row]] objectAtIndex:0];

Where does the program crash, what line?

Jordan
  • 21,746
  • 10
  • 51
  • 63
  • As displayed, the program does not crash, but does not pull the string from the plist. If I uncomment the second line where i print to NSLog "Strings from plist" This will crash it. However in the method before it, the same call to NSLog will function properly and display the strings in the console. I am not at my Mac at the moment, but will try your call and see if it renders what I am looking to achieve. – Nungster Oct 05 '10 at 14:07
  • In the post, you state Bad Access. On what line? – Jordan Oct 05 '10 at 14:28
  • I tried this line and I still get the EXC_BAD_ACCESS at this line as before. :( – Nungster Oct 05 '10 at 21:39
  • Before calling this line use NSLog to print values for adictionary. – Jordan Oct 06 '10 at 13:08