ヒレガス本Chapter5
しゃべるアプリです。
AppController.h
#import <Cocoa/Cocoa.h> @interface AppController : NSObject { IBOutlet NSTextField *textField; NSSpeechSynthesizer *speechSynth; } -(IBAction)sayIt:(id)sender; -(IBAction)stopIt:(id)sender; @end
AppController.m
#import "AppController.h" @implementation AppController -(id)init { [super init]; NSLog(@"init"); speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil]; return self; } -(IBAction)sayIt:(id)sender { NSString *string = [textField stringValue]; if([string length] == 0) { NSLog(@"string from %@ is of zero-length", textField); return; } [speechSynth startSpeakingString:string]; NSLog(@"Have started to say: %@", string); } -(IBAction)stopIt:(id)sender { NSLog(@"stopping"); [speechSynth stopSpeaking]; }
こちらは演習
AppController.h
#import <Cocoa/Cocoa.h> @interface AppController : NSObject { IBOutlet NSTextField *inputTextField; IBOutlet NSTextField *outputTextField; } -(IBAction)count:(id)sender; @end
AppController.m
#import "AppController.h" @implementation AppController -(IBAction)count:(id)sender { NSString *string = [inputTextField stringValue]; [outputTextField setStringValue:[NSString stringWithFormat:@"%@ has a %d characters.", string, [string length]]]; } @end