Wrox Programmer Forums
|
BOOK: Beginning iOS 4 Application Development
This is the forum to discuss the Wrox book Beginning iOS 4 Application Development by Wei-Meng Lee; ISBN: 978-0-470-91802-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 25th, 2011, 07:40 AM
Registered User
 
Join Date: Apr 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default chapter 8

this one crashes too

again xcode isn't showing any code errors/warnings

app crashes at launch with this error:
Quote:
2011-04-25 12:38:42.115 TableView[530:207] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x4b1d7c0
2011-04-25 12:38:42.117 TableView[530:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x4b1d7c0'
on this line
Code:
cell.textLabel.text = [movieSection objectAtIndex:[indexPath row]];
this is the file content:
Code:
//
//  RootViewController.m
//  TableView
//
//  Created by Martin Chrobot on 24/04/2011.
//  Copyright 2011 home. All rights reserved.
//

#import "RootViewController.h"

@implementation RootViewController

@synthesize movieTitles;
@synthesize years;
@synthesize searchBar;

- (void)viewDidLoad
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Movies" ofType:@"plist"];
    
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:path];
    
    self.movieTitles = dic;
    
    [dic release];
    
    NSArray *array = [[self.movieTitles allKeys] sortedArrayUsingSelector:@selector(compare:)];
    
    self.years = array;
    
    self.tableView.tableHeaderView = searchBar;
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeYes;
    
    [super viewDidLoad];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return years;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //return 1;
    return [self.years count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return 0;
    
    NSString *year = [self.years objectAtIndex:section];
    
    NSArray *movieSection = [self.movieTitles objectForKey:year];
    
    return [movieSection count];
}

// Customize the appearance of table view cells.
- (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];
    }

    // Configure the cell.
    NSString *year = [self.years objectAtIndex:[indexPath section]];
    
    NSArray *movieSection = [self.movieTitles objectForKey:year];
    
    cell.textLabel.text = [movieSection objectAtIndex:[indexPath row]];
    
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *year = [self.years objectAtIndex:section];
    return year;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
	*/
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc
{
    [movieTitles release];
    [years release];
    [searchBar release];
    [super dealloc];
}

@end
any ideas what's wrong?
 
Old May 1st, 2011, 11:11 AM
Friend of Wrox
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

The chapter 10 reply is a little more in depth, and caused by the same issue - an additional layer in the plist. A dictionary containing a dictionary of arrays, instead of just a dictionary of arrays.

The following demonstrates what is going on, with added lines and comments in bold

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

    // Configure the cell.
	NSLog(@"self.years is class%@\nThe contents of years is %@", [self.years class], self.years); //The array contains one object a dictionary called "Root" not the list of years as planned
    NSString *year = [self.years objectAtIndex:[indexPath section]];
	NSLog(@"year is %@",year); // Year is not a year, it is the only key "Root"
	NSLog(@"self.movieTitles is %@",[self.movieTitles class]); //movieTitles is a dictionary as expected
    
    NSArray *movieSection = [self.movieTitles objectForKey:year];//The objectForKey:year is the object for key "Root" which is a dictionary, not an array
	NSLog(@"movieSection contents %@\nmovieSection class %@",movieSection, [movieSection class]); // listing the contents - it is a dictionary of the year arrays and their movie strings. The class is __NSCFDictionary
	
    
    cell.textLabel.text = [movieSection objectAtIndex:[indexPath row]];//Crash because movieSection is supposed to be an array and is not - it is a dictionary. The dictionary class does not have an "objectAtIndex:" method
    
    return cell;
}
To resolve the problem the extra layer in the plist needs to be removed. See
http://p2p.wrox.com/271591-post2.html
for the simplest way to do this.

Bob





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 6 - Code Download Missing for this Chapter dbaechtel BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 0 August 11th, 2009 11:02 AM
Chapter 2 - End of chapter exercises whizzkid1892 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 30th, 2008 12:02 PM
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.