Note that there are some explanatory texts on larger screens.

plurals
  1. POobjectForKey returns nil on a dictionary
    primarykey
    data
    text
    <p>I am creating a recipe manager app for iPad. I am using the split view template which loads data for the recipes from a plist which is structured in this way. The table view on the left works perfectly. It loads the data from the plist. The problem is the detail view. As you can see I have inserted a NSLog lines in the DetailViewController.m file since every string I passed to the text field resulted as nil. Strangely, when I tried to NSLog from the RootViewController.m with: NSLog(@"%@", [detailViewController.ricetta allKeys]) it worked.</p> <p>Any suggestions?</p> <p>I tried to post as much lines as possible to explain the problem as well as possible.</p> <pre><code>&lt;plist version="1.0"&gt; &lt;array&gt; &lt;dict&gt; &lt;key&gt;name&lt;/key&gt; &lt;string&gt;Risotto alla zucca&lt;/string&gt; &lt;key&gt;ingredients&lt;/key&gt; &lt;string&gt;&lt;/string&gt; &lt;key&gt;description&lt;/key&gt; &lt;string&gt;&lt;/string&gt; &lt;key&gt;photo&lt;/key&gt; &lt;string&gt;&lt;/string&gt; &lt;/dict&gt; &lt;dict&gt; &lt;key&gt;name&lt;/key&gt; &lt;string&gt;Semi di zucca al forno&lt;/string&gt; &lt;key&gt;ingredients&lt;/key&gt; &lt;string&gt;&lt;/string&gt; &lt;key&gt;description&lt;/key&gt; &lt;string&gt;&lt;/string&gt; &lt;key&gt;photo&lt;/key&gt; &lt;string&gt;&lt;/string&gt; &lt;/dict&gt; &lt;dict&gt; &lt;key&gt;name&lt;/key&gt; &lt;string&gt;Risotto ai funghi&lt;/string&gt; &lt;key&gt;ingredients&lt;/key&gt; &lt;string&gt;&lt;/string&gt; &lt;key&gt;description&lt;/key&gt; &lt;string&gt;&lt;/string&gt; &lt;key&gt;photo&lt;/key&gt; &lt;string&gt;&lt;/string&gt; &lt;/dict&gt; &lt;/array&gt; &lt;/plist&gt; </code></pre> <p><strong>RootViewController.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "RecipeConstants.h" @class DetailViewController; @interface RootViewController : UITableViewController { NSMutableArray *ricette; } @property (nonatomic, retain) NSMutableArray *ricette; @property (nonatomic, retain) IBOutlet DetailViewController *detailViewController; @end </code></pre> <p><strong>RootViewController.m</strong></p> <pre><code>... @synthesize ricette; - (void)viewDidLoad { [super viewDidLoad]; self.clearsSelectionOnViewWillAppear = NO; self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0); NSString *path = [[NSBundle mainBundle] pathForResource:@"RicetteArray" ofType:@"plist"]; NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path]; self.ricette = tmpArray; [tmpArray release]; } ... - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.ricette count]; } // Configure the cell. cell.textLabel.text = [[self.ricette objectAtIndex:indexPath.row] objectForKey:NAME_KEY]; return cell; } ... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { detailViewController.ricetta = [self.ricette objectAtIndex:indexPath.row]; } ... - (void)dealloc { [detailViewController release]; [ricette release]; [super dealloc]; } @end </code></pre> <p><strong>DetailViewController.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "RecipeConstants.h" @interface DetailViewController : UIViewController &lt;UIPopoverControllerDelegate, UISplitViewControllerDelegate&gt; { IBOutlet UILabel *lblTitolo; IBOutlet UITextView *ingredientiTxtView; IBOutlet UITextView *descrizioneTxtView; IBOutlet UIImageView *fotoImgView; NSDictionary *ricetta; } @property (nonatomic, retain) IBOutlet UILabel *lblTitolo; @property (nonatomic, retain) IBOutlet UITextView *ingredientiTxtView; @property (nonatomic, retain) IBOutlet UITextView *descrizioneTxtView; @property (nonatomic, retain) IBOutlet UIImageView *fotoImgView; @property (nonatomic, retain) NSDictionary *ricetta; @property (nonatomic, retain) IBOutlet UIToolbar *toolbar; @property (nonatomic, retain) id detailItem; @end </code></pre> <p><strong>DetailViewController.m</strong></p> <pre><code>@synthesize lblTitolo, ingredientiTxtView, descrizioneTxtView, fotoImgView, ricetta; ... - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; lblTitolo.text = [ricetta objectForKey:NAME_KEY]; ingredientiTxtView.text = [ricetta objectForKey:INGREDIENTS_KEY]; descrizioneTxtView.text = [ricetta objectForKey:DESCRIPTION_KEY]; UIImage *img = [UIImage imageNamed:PHOTO_KEY]; fotoImgView.image = img; NSLog(@"%@", [ricetta allKeys]); } ... - (void)dealloc { [_myPopoverController release]; [_toolbar release]; [_detailItem release]; [lblTitolo release]; [ingredientiTxtView release]; [descrizioneTxtView release]; [fotoImgView release]; [ricetta release]; [super dealloc]; } @end </code></pre> <p>UPDATE: Actually in the DetailViewController.m I have this method:</p> <pre><code>- (void)updateRicetta { lblTitolo.text = [ricetta objectForKey:NAME_KEY]; NSString *myIngredients = [ricetta objectForKey:INGREDIENTS_KEY]; myIngredients = [myIngredients stringByReplacingOccurrencesOfString:@", " withString:@"\n"]; ingredientiTxtView.text = myIngredients; NSString *myDescription = [ricetta objectForKey:DESCRIPTION_KEY]; myDescription = [myDescription stringByReplacingOccurrencesOfString:@". " withString:@"\n"]; descrizioneTxtView.text = myDescription; NSString *nomeImmagine = [NSString stringWithFormat:@"%@.jpg", [ricetta objectForKey:PHOTO_KEY]]; [self.fotoImgView setImage:[UIImage imageNamed:nomeImmagine]]; } </code></pre> <p>which is called from RootViewController.m in</p> <pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath </code></pre> <p>using</p> <pre><code>[detailViewController updateRicetta]; </code></pre> <p>I have the following two warnings:</p> <pre><code>'DetailViewController' may not respond to '-updateRicetta' Instance method '-updateRicetta not found (return type defaults to 'id') </code></pre> <p>Another problem is that the detail view updates only when I click on a cell not at app loading.</p>
    singulars
    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.
 

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