我一直在写一本关于stackoverflow的书中的练习,但是我遇到了一个问题,经过三天的攻击,我认为我需要一些帮助。
我正在完成“Speakline”练习,我们在界面中添加了一个TableView,表格将显示您可以为该程序的文本到语音方面选择的“声音”。
我有两个问题,我似乎无法深究:
我觉得这两个问题都是相关的。
我在这里包含我的界面代码:
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject <NSSpeechSynthesizerDelegate, NSTableViewDelegate>
{
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
我的实施代码在这里:
#import "AppController.h"
@implementation AppController
- (id)init
{
[super init];
//Log to help me understand what is happening
NSLog(@"init");
speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
[speechSynth setDelegate:self];
voiceList = [[NSSpeechSynthesizer availableVoices] retain];
return self;
}
- (IBAction)sayIt:(id)sender
{
NSString *string = [[textField stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//Is the string zero-length?
if([string length] == 0) {
NSLog(@"String from %@ is a string with a length of %d.", textField, [string length]);
[speechSynth startSpeakingString:@"Please enter a phrase first."];
}
[speechSynth startSpeakingString:string];
NSLog(@"Started to say: %@", string);
[stopButton setEnabled:YES];
[startButton setEnabled:NO];
}
- (IBAction)stopIt:(id)sender
{
NSLog(@"Stopping...");
[speechSynth stopSpeaking];
}
- (void) speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)complete
{
NSLog(@"Complete = %d", complete);
[stopButton setEnabled:NO];
[startButton setEnabled:YES];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [voiceList count];
}
- (id)tableView: (NSTableView *)tv objecValueForTableColumn: (NSTableColumn *)tableColumn
row:(NSInteger)row
{
NSString *v = [voiceList objectAtIndex:row];
NSLog(@"v = %@",v);
NSDictionary *dict = [NSSpeechSynthesizer attributesForVoice:v];
return [dict objectForKey:NSVoiceName];
}
/*
- (BOOL)respondsToSelector:(SEL)aSelector
{
NSString *methodName = NSStringFromSelector(aSelector);
NSLog(@"respondsToSelector: %@", methodName);
return [super respondsToSelector:aSelector];
}
*/
@end
希望你们能看到一些我错过的东西。
谢谢!
答案 0 :(得分:3)
objecValueForTableColumn
与objectValueForTableColumn
不同。对于委托和数据源,我建议永远不要键入方法名称,如果你可以避免它 - 它会导致这种问题。如果你复制&amp;将方法签名粘贴到您可以更安全的文档中。祝你学习顺利!