views:

1292

answers:

1

I have been trying to get this to work for about three solid days and get my mind around it. Can someone advise. I have built the basics and they all work great but when I try to do that extra bit I cannot get my head around it. I am trying to build a table of technical terms, from a plist. This is an indexed and sections table by the alphabet. This works fine but when I then try to add the next level for each term's definition in a new viewcontroller I can't seem to get the code or the plist structure right. At the moment I have created two plists. One with a dictionary of the alphabet in 26 arrays, within each array is a series of technical terms. All this works great. Then I've created another plist of definitions as an array of dictionaries, one for each word/definition pair. I'm expecting to be passing the @"word" key from the view controller to the detailviewcontroller then picking up the @"definition". I don't know whether this is right or wrong(?) My code shows the technical term table great but when a row is selected it crashes. I know it's to do with the code for passing the detailviewcontroller's reference so the detailview can pick up the definition - but I've no idea how to solve it. I've posted parts of my code here for someone to look at help. Any ideas?

NSString *wordPath = [[NSBundle mainBundle] pathForResource:@"newsortedglossary" ofType:@"plist"];
NSDictionary *wordDict = [[NSDictionary alloc] initWithContentsOfFile:wordPath];
self.words = wordDict;
[words release];
NSArray *wordArray = [[words allKeys] sortedArrayUsingSelector:@selector(compare:)];
self.wordKeys = wordArray;

NSString *definitionPath = [[NSBundle mainBundle] pathForResource:@"newnewdefinitionglossary" ofType:@"plist"];
NSDictionary *definitionDict = [[NSDictionary alloc] initWithContentsOfFile:definitionPath];
self.definitions = definitionDict;
[definitions release];

didSelectRow here.........

GlossaryDetailViewController *glossaryDetailViewController = [[GlossaryDetailViewController alloc]
                 initWithNibName:@"GlossaryDetailView" bundle:nil];

NSLog(@"did select-2"); // CRASHES HERE with NSDictionary may not respond to objectAtIndex
glossaryDetailViewController.definition = [self.words objectAtIndex:indexPath.row];
NSLog(@"did select-3");
[self.navigationController pushViewController:glossaryDetailViewController animated:YES];
NSLog(@"did select-4");
[glossaryDetailViewController release];

detailViewController here.......

- (void)viewDidAppear:(BOOL)animated {
NSLog(@"didappear");
self.glossaryWordDefinition.text = [definition objectForKey:@"definition"];
A: 

It seems that you are trying to access the members of dictionary by using an index, instead of using a key to lookup the associated value.

In your didSelectRow you probably want this:

glossaryDetailViewController.definition = [self.wordKeys objectAtIndex:indexPath.row];

The difference is that now you are trying access the members of an array with the index.

mahboudz
Thanks a lot. I'm sure that you are right. I've changed it and the code now processes through to the detailcontroller. But then I think it crashes there, in the code..NSLog(@"Glossary detail viewDidLoad start"); self.navigationItem.title = [definition objectForKey:@"word"]; [super viewDidLoad];
Maxwell Segal
Ok, so I guess you are trying to get the definition of the key, "word" for that array entry. I think you may be a little unclear on the concept of dictionary versus arrays. NSArrays are a list of objects, usually more than one item per array. NSDictionaries are pairs of keys and their associated values. The keys are going to be unique. For example, there will only be one key="word" in the whole dictionary. It looks like you might be thinking that there is a "word" key for every index, and that tells me that you might be mistaking an array for a dictionary.
mahboudz
An array can have an unlimited number of dictionaries as its entries. And once you pass an object from the array to your detail controller, then you could look for the key="word" in that one entry. But the way you loaded the dictionary above doesn't look like that kind of usage. You prbably want to load your array like this: NSArray tempArray = [[NSArray alloc] initWithContentsOfFile: wordPath]; Now each entry that you get out with objectAtIndex: is going to be an independent dictionary and you can extract your key pairs as you wish. This is the best I can do with the code you have provided.
mahboudz
Happy holidays to you! Your explanation has helped me with two things. First, solve my problem and second, better understand arrays and dictionaries. I've now changed the definition data to an array and then read it into my detail controller perfectly. Thanks.
Maxwell Segal
Hold it. The definitions do now appear in the detail view but seem confined only to 'sections' In that, if I select the second term in section 'B" it brings up the definition for the second term definition for section 'A". This happens throughout all sections - everything comes up with letter A's term definitions. Obviuously I should be telling it which section?, but how?
Maxwell Segal
I need to know more about your data structure. You have one large array with dictionary entries, right? How are they related to sections? Is each array entry a different section?
mahboudz