Alexander Bollbach
Alexander Bollbach

Reputation: 659

Missing context for method declaration in main.m file?

I am trying to declare and define a method/function in objective C in a main.m file. The reason I'm in a main file and not some class implementation is because I chose "command line Mac project" as I am doing this for a simple word processing exercise.

I have declared the method and defined it.

     - (float) findAverageWordsInFile(NSString *)pathToFile;

I am then defining/implementing my method beneath the main function and attempting to call it inside main. There are definitely some idiosyncrasies to method creation in objective C that I am missing. After googling this for a while I can't seem to figure this out.

Upvotes: 2

Views: 1186

Answers (1)

TylerP
TylerP

Reputation: 9829

Objective-C methods can only be defined in Objective-C classes. However, you can use a C function instead. The main function itself is an example of this:

int main(int argc, char * argv[]) {
    // ...
}

So, your function would be something like:

float findAverageWordsInFile(NSString *pathToFile) {
    // ...
}

Upvotes: 3

Related Questions