simple trouble with XCode Obj-C method definition

Discussion in 'Mac Programming' started by Oats, Jul 29, 2007.

  1. macrumors regular

    Joined:
    Jan 8, 2003
    Location:
    New York
    #1
    I am relatively new to Objective-C and XCODE. I know C++ very well, but objective-C is giving me some headaches so far. I am trying to define a simple class, with one method / function which has two parameters, searchStr and searchPath. However, when I try to use this method, I get an error that indicates that the method could not be found or called.

    Here is my search.h file:
    PHP:
    #import <Cocoa/Cocoa.h>

    @interface CSearch NSObject
        
    - (NSMutableArray *) searchForFiles:(NSString *)searchStr path:(NSString *)searchPath;

    @
    end
    here is the search.m file:
    PHP:
    #import "Search.h"

    @implementation CSearch

    - (idinit { return self; }

    - (
    voiddealloc { [super dealloc]; }

    //
    // SEARCH FOR FILES
    //////////////////////////////////////////////////////
    - (NSMutableArray *) searchForFiles:(NSString *)searchStr path:(NSString *)searchPath
    {
        ...
    }

    @
    end
    and finally, here is the portion of code where my function is called:
    PHP:
        CSearch  searchClass;
        
    NSString *searchStr = @"searchstring";
        
    NSString *filePathSearchStr = @"testpath";
        [
    searchClass searchForFiles:searchStr path:filePathSearchStr];
    The project compiles with no warnings or errors, but when I try to call the searchForFiles method, it can't be found or hangs in debug mode. Any idea what I am doing wrong?
    Thanks!!
     
  2. macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #2
    Yeah, searchClass is not initialized in the last code block.
    You need to do something like this for the first line:
    Code:
    CSearch  * searchClass = [[CSearch alloc] init];
    
    Your current code is equivalent to:
    Code:
       CSearch  * searchClass;
        char searchStr[] = "searchstring"; 
        char filePathSearchStr[] = "testpath"; 
       searchClass->searchForFiles(searchStr, filePathSearchStr);
    
    in C++
     
  3. thread starter macrumors regular

    Joined:
    Jan 8, 2003
    Location:
    New York
    #3
    indeed you are correct. thanks so much!!
     
  4. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #4
    Don't you need to call [ super init ] in your init method?

    b e n
     

Share This Page