iOS app (this class is not key value coding-compliant for the key dataSource)

12,887

Solution 1

It looks like you've wired things up in interface builder incorrectly. It appears you have attached something to an outlet called dataSource in your SessionsTableViewController. It looks like you probably wanted to do it the other way round as I assume you have a table view in SessionsTableViewController.

So, you need to attach the dataSource property of your table view to your instance of SessionsTableViewController (probably "File's Owner" in your case), rather than the other way round.

Solution 2

This error usually occur when outlets are not connected properly. In my case checking outlets connection would always solve it.

Share:
12,887

Related videos on Youtube

Sander Declerck
Author by

Sander Declerck

"Cycling Coffee loving know it all in a good way sharing is caring genius" - Laura Landuyt

Updated on June 04, 2022

Comments

  • Sander Declerck
    Sander Declerck almost 2 years

    I'm making an iPhone app, and I'm working at this TableViewController, but when testing, I get this error, and I don't really know what to do with it:

    2012-01-13 13:45:32.947 HandHistory Reviews[3422:707] *** 
    Terminating app due to uncaught exception 'NSUnknownKeyException', 
    reason: '[<SessionsTableViewController 0x191cb0> setValue:forUndefinedKey:]: 
    this class is not key value coding-compliant for the key dataSource.'
    

    Anyone got an idea?

    This is my SessionTableViewController.m file:

    #import "SessionsTableViewController.h"
    #import "Session.h"
    
    @interface SessionsTableViewController()
    
    @property (nonatomic, copy) NSArray *sessions;
    
    @end
    
    @implementation SessionsTableViewController
    
    @synthesize sessions = _sessions;
    
    #pragma mark - View lifecycle
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        NSMutableArray *sessions = [[NSMutableArray alloc] init];
    
        // Looking for files
        // Finding the Documents directory
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [paths objectAtIndex:0];
        NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
    
        // Looping through each file in the directory
        for (NSString *file in directoryContent)
        {
            NSString *contents = [NSString stringWithContentsOfFile:[[paths objectAtIndex:0] stringByAppendingPathComponent:file] encoding:NSUTF8StringEncoding error:nil];
    
            Session *session = [[Session alloc] initWithName:file andContents:contents];
    
            [sessions addObject:session];
        }
    
        self.sessions = sessions;
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [self.sessions count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Session Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        //cell.textLabel.text = [[self.sessions objectAtIndex:indexPath.row] name];
        //cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%d hands", 10];
    
        return cell;
    }
    
    
    
    #pragma mark - Table view delegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Navigation logic may go here. Create and push another view controller.
        /*
         <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
         // ...
         // Pass the selected object to the new view controller.
         [self.navigationController pushViewController:detailViewController animated:YES];
         */
    }
    
    @end
    
  • Sander Declerck
    Sander Declerck over 12 years
    I see, I've set my SessionsTableViewController as custom class for my TableView instead of my TableViewController, thanks, this is what I was looking for, everything works now.
  • Tim Ogilvy
    Tim Ogilvy almost 8 years
    If this comment answers the question, it's not immediately clear how. Consider rewording?
  • music_and_cities
    music_and_cities over 7 years
    I don'n know how else I can reword it. If you have anything specified in Module, you will get the error the OP specified when you try to build another module.