What's the correct way to define a C function in an Objective-C project?

I've inherited a legacy codebase. There's a C function declared in a header file alongside an enum:

Code Block
//SomeFile.h
typedef enum: NSUInteger {
// cases declared
} MyEnum;
static NSString* StringForEnum(MyEnum enumType)
{
NSString *stringRep = nil
    switch(enumType) {
// set stringRep to string depending on case
}
return stringRep;
}


Building the project I get the following warning:

Unused function 'StringForEnum'

But it is used, in an Objective-C class and also in a Unit Test, which verifies that the switch statement returns the correct string.

From some googling, I've found some recommendations to declare the function as static inline instead. I've also seen that functions I know about such as NSMakeRange are declared with NSINLINE (omitting the underscore as it appears to break markdown on the forum), which appears to be a directive that does similar.

However if I change the signature to NSINLINE, the existing Unit Test fails - I can't figure out why, because setting a breakpoint in the function does not work.

So my question is - am I doing this right? I wondered if a I needed static at all, but if I don't include something I get linking errors.


Replies

The static keyword is usually used to define global objects or functions that are only visible in the file where they are defined. I'm not sure what the point of having that in a header would be.

I suggest moving the function to an Objective-C file. Remove the static. In the header, use extern instead.
I'm not sure what the point of having that in a header would be.
It’s commonly used in concert with inline. But in general I agree with your opinion that this would be better declared extern.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"