Reputation: 10971
I am trying to put a C-style function in the header of an Objective-C class. (My terminology might be wrong here -- I'm just used to writing Objective-C class methods rather than functions). It looks as follows:
// Sort function
NSInteger sort(NSString *aString, NSString *bString, void *context);
NSInteger sort(NSString *aString, NSString *bString, void *context) {
return [aString compare:bString options:NSNumericSearch];
}
Unforuntately this results in:
Expected '=', ',', ';', 'asm' or 'attribute' before '{' token
Any ideas as to what I'm missing? Thank you.
Upvotes: 6
Views: 6880
Reputation: 69302
The body of your function needs to be in the .m file instead of in the header.
As long as the declaration of your function (NSInteger sort(NSString *aString, NSString *bString, void *context);
) remains in the header you'll still be able to access the sort function from anywhere you import the header.
Upvotes: 2
Reputation: 891
When declaring C-Styled methods you must forget about - or +. Just declare the method as an standard C one, before the @end
statement:
void function_name(int, int);
Upvotes: 2
Reputation: 64477
My guess is that you put the function definition within the @interface of your class. Instead, make sure 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
Upvotes: 11