js_
js_

Reputation: 4731

C function and variable inside Objective-C class implementation?

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?

Upvotes: 7

Views: 1988

Answers (4)

user1985657
user1985657

Reputation:

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.

Upvotes: 0

Motti Shneor
Motti Shneor

Reputation: 2194

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".

Upvotes: 1

user529758
user529758

Reputation:

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.

Upvotes: 2

kuba
kuba

Reputation: 7389

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.

Upvotes: 3

Related Questions