Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 378
js_ Avatar asked Nov 16 '12 15:11

js_


1 Answers

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.

like image 66
kuba Avatar answered Oct 26 '22 01:10

kuba