7

I can define c function and variable inside Objective-C class implementation like this:

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    cfunction();
}

// variable inside class
NSString *message = @"this is c function inside objective-c class";

// c function inside class
void cfunction() {
    NSLog(@"%@", message);
}

@end

RESULT

this is c function inside objective-c class

This code works without any warnings and errors.
Is it valid Objective-C code?
Is it safe to define variable and c function inside Objective-C class?
Are there documentations about this language specifications?

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
js_
  • 4,671
  • 6
  • 44
  • 61

4 Answers4

3

Yes, this is correct and safe, but you should try to avoid it. If you need to have some C-function, define it in a separate file (and declare it in a separate header file).

Technically, Obj-C is a thin layer on top of the C language and you can use any C feature anywhere in a Obj-C source code. However you should conform to the coding style and not mix the two languages.

kuba
  • 7,329
  • 1
  • 36
  • 41
  • 1
    "you should conform to the coding style and not mix the two languages." - *which* coding style? How *not to mix* the "two" languages? Is it bad style if I write `- (int)foo { return 1 + 2; }`, just because `1 + 2` is a C expression itself? –  Nov 16 '12 at 15:08
  • I meant not to mix e.g. C-structs and Obj-C objects. Not to mix C-functions and Obj-C methods. – kuba Nov 16 '12 at 15:16
  • @kuba thanks! In normal C, `cfunction()` must define before `-viewDidLoad`. But if I define `cfunction()` between @implementation and @end, `cfunction()` can be defined after `-viewDidLoad`. And as H2CO3 said, `functions can directly access private and protected ivars of an object`. I think defining c-function inside @implementation is more than c language. But I can't find documentation. – js_ Nov 16 '12 at 15:53
2

Is it valid Objective-C code?

Yes, it is.

Is it safe to define variable and c function inside Objective-C class?

Yes, it is. One particular property of functions defined within classes is that such functions can directly access private and protected ivars of an object which is instance of the specified class.

Are there documentation about this language specification?

See the discussion around this SO question.

Community
  • 1
  • 1
  • 1
    thanks. but i couldn't find the documentation in [the SO question](http://stackoverflow.com/questions/801976/mixing-c-functions-in-an-objective-c-class) . If there is no documentation, is it dangerous to rely on that feature? – js_ Nov 16 '12 at 15:33
1

Sorry guys, previous answer is very wrong in its third part.

C functions implemented within the @implementation of an objective-C class are NOT methods of that class, but rather simple, global C functions.

Of course it is OK to write them there, and sometimes even beneficial because they're "private to the class" -- other code will not see them, as they are scoped within the @implementation.

However - they can not access any iVars, or self, or super, or anything related to the class or instance. They are C functions and not Obj-C methods.

Any attempt to access iVars or self or super etc. will produce a compiler error saying "symbol xxx is unknown".

Motti Shneor
  • 2,095
  • 1
  • 18
  • 24
0

In addition to all the other answers (especially Motti's):

You can prefix the C-function by the 'static' keyword. 'static' means that it will only be possible to use that function from within the C file it's present in. It will not be possible to export it or call it from outside that C-file. Eg. If you have a static function in a header-file, you will get multiple copies of it.

It's as simple as this:

Prototype:

static void cfunction();

Implementation:

static void cfunction() {
    NSLog(@"%@", message);
}

-Now try to call it from code in a different source-file.

...And there's nothing wrong with mixing Objective-C and C. Generally speaking: Objective-C is sluggish. C++ is slow and C is quite fast. -So if you have subroutines (or inline functions) that you call a lot, make those C functions instead of Objective-C, because ObjC will drag your loops down.