PDA

View Full Version : simple trouble with XCode Obj-C method definition




Oats
Jul 29, 2007, 09:21 PM
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:

#import <Cocoa/Cocoa.h>

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

@end


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

@implementation CSearch

- (id) init { return self; }

- (void) dealloc { [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:

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!!



iSee
Jul 29, 2007, 10:50 PM
Yeah, searchClass is not initialized in the last code block.
You need to do something like this for the first line:

CSearch * searchClass = [[CSearch alloc] init];


Your current code is equivalent to:

CSearch * searchClass;
char searchStr[] = "searchstring";
char filePathSearchStr[] = "testpath";
searchClass->searchForFiles(searchStr, filePathSearchStr);

in C++

Oats
Jul 29, 2007, 11:00 PM
Yeah, searchClass is not initialized in the last code block.
You need to do something like this for the first line:

CSearch * searchClass = [[CSearch alloc] init];



indeed you are correct. thanks so much!!

lazydog
Jul 30, 2007, 03:29 AM
Don't you need to call [ super init ] in your init method?

b e n