A.13. Chapter 13

A.13.1. Exercise solution

  1. Open the Searcher project in Xcode, and select the Searcher.h file in the Groups & Files panel, such that it appears in the editor. Make the highlighted changes in the following code:

    #import <Cocoa/Cocoa.h>
    
    @interface Searcher : NSObject
    {
        IBOutlet NSArrayController *pathsController;
        NSArray *paths;
        NSString *rootDirectoryPath;
        NSString *nameContainsString;
        int searchingState;
        NSTask *task;
    }
    
    -(IBAction)openFile:(id)sender;
    -(IBAction)showInFinder:(id)sender;
    -(IBAction)toggleSearch:(id)sender;
    
    -(NSArray *)paths;
    -(void)setPaths:(NSArray *)paths;
    
    -(NSString *)rootDirectoryPath;
    -(void)setRootDirectoryPath:(NSString *)path;
    
    -(NSString *)nameContainsString;
    -(void)setNameContainsString:(NSString *)str;
    
    -(NSTask *)task;
    -(void)setTask:(NSTask *)task;
    
    -(int)searchingState;
    -(void)setSearchingState:(int)newState;
    
    @end
  2. Open the Searcher.m file in the editor and change the init and dealloc methods as follows:

    -(id)init { if ( self = [super init] ) { [self setPaths:[NSArray array]]; [self setRootDirectoryPath:NSHomeDirectory()]; [self setNameContainsString:@""]; [self setSearchingState:NSOffState]; [self setTask:nil]; // Register for NSTask termination notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskDidTerminate:) name:NSTaskDidTerminateNotification object:nil]; } return self; } -(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; [paths release]; [rootDirectoryPath release]; ...

Get Beginning Mac OS® X Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.