Declaring Obj-C Function in Title

I am trying to put a C-style function in the title of an Objective-C class. (My terminology may be wrong here - I'm just used to writing methods of the Objective-C class, not functions). It looks like this:

// Sort function
NSInteger sort(NSString *aString, NSString *bString, void *context);

NSInteger sort(NSString *aString, NSString *bString, void *context) {
    return [aString compare:bString options:NSNumericSearch];
}

This directly leads to:

Expected '=', ',', ';', 'asm' or 'attribute' before '{' token

Any ideas on what I am missing? Thank.

+5
source share
3 answers

I assume that you put the function definition in the @interface of your class. Instead, make sure that C style function declarations are outside of Objective-C @interface declarations:

// declare C functions here
NSInteger sort(NSString *aString, NSString *bString, void *context);

@interface MyClass : NSObject
{
  // class instance vars
}

// class properties & instance methods
@end
+11

.m, .

(NSInteger sort(NSString *aString, NSString *bString, void *context);) , , .

+2

When declaring C-Styled methods, you should forget about - or +. Just declare the method as the standard C one, before the statement @end:

void function_name(int, int);

+1
source

All Articles