Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Samppaa

macrumors regular
Original poster
Mar 26, 2010
168
23
Hey, I am reading the cocoa programming for mac os x third edition and in chapter 6 I got a problem when I try to make the tableView show data console tells error
Code:
2010-06-07 23:46:29.816 SpeakLine[14780:a0f] init
2010-06-07 23:46:29.920 SpeakLine[14780:a0f] *** Illegal NSTableView data source (<AppController: 0x200046040>).  Must implement numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:

I don't understand as I got those methods, code comes here:
AppController.h
Code:
//
//  AppController.h
//  SpeakLine
//
//  Created by Samuli Lehtonen on 1.6.2010.
//  Copyright 2010 Test. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface AppController : NSObject {
	IBOutlet NSTextField *textField;
	NSSpeechSynthesizer *speechSynth;
	
	IBOutlet NSButton *stopButton;
	IBOutlet NSButton *startButton;
	
	IBOutlet NSTableView *tableView;
	NSArray *voiceList;

}

-(IBAction)sayIt:(id)sender;
-(IBAction)stopIt:(id)sender;

@end

AppController.m
Code:
//
//  AppController.m
//  SpeakLine
//
//  Created by Samuli Lehtonen on 1.6.2010.
//  Copyright 2010 Test. All rights reserved.
//

#import "AppController.h"


@implementation AppController

-(id)init
{
	[super init];
	NSLog(@"init");
	
	speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
	[speechSynth setDelegate:self];
	voiceList = [[NSSpeechSynthesizer availableVoices] retain];
	return self;
}

-(int)numberOfRowsInTableView:(NSTableView *)tv
{
	return [voiceList count];
}

-(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumnrow:(int)row
{
	NSString *v = [voiceList objectAtIndex:row];
	return v;
}

-(void)speechSynthesizer:(NSSpeechSynthesizer *)sender
didFinishSpeaking:(BOOL)complete
{
	NSLog(@"complete = %d, complete");
	[stopButton setEnabled:NO];
	[startButton setEnabled:YES];
}
	

-(IBAction)sayIt:(id)sender
{
	NSString *string = [textField stringValue];
	
	if ([string length] == 0) {
		NSLog(@"String is zero lenght");
		return;
	}
	
	[speechSynth startSpeakingString:string];
	[stopButton setEnabled:YES];
	[startButton setEnabled:NO];
	
}

-(IBAction)stopIt:(id)sender
{
	[speechSynth stopSpeaking];
}


@end
 
I think you accidentally selected "Command line tool" instead of "Cocoa Application" when you created the project. You'll note that AppController.h says;

Code:
#import <Foundation/Foundation.h>

While it should say;

Code:
#import <Cocoa/Cocoa.h>

Unfortunately you can't just change that line, you're likely not getting the proper frameworks either, so just restart the project, and make sure you select the right kind of application.
 
Well the program works fine, but only that tableview bugs... So it's not from that.. I did as exactly in book.
 
Well, to be honest, i'm using the same book, and that's the first difference I see. Other than that, didyou double check the datasource connection in Interface Builder?
 
Yes checked and if I create a new class it automatically includes the foundation. An Objective-C class file, with an optional header which includes the <Foundation/Foundation.h> header. Some suggestions?
 
Here's a little typo you made. Might be the source of your error.

Code:
-(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumnrow:(int)row

You forgot the space between "tableColumn" and "row:"
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.