J. Malone
J. Malone

Reputation: 43

Mixing C and objective C in xcode

I'm very new to Xcode and objective C, I know the basics but I was wondering if you can implement normal C code into objective C in Xcode, I saw somewhere that you can create a C header file and implement that into Xcode?? if that's possible, how would you do that, i'm not very experienced with headers. If that completely wrong I would generally just like to know how to call C functions in objective C (Xcode)

Upvotes: 1

Views: 657

Answers (3)

user8263284
user8263284

Reputation:

Objective c can call C in-line. For instance:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext(); //result of a C method
    [self drawRoundedRectWithContext:context withRect:rect];

}

Upvotes: 1

Shah Nilay
Shah Nilay

Reputation: 798

Try with this way.

void *refToSelf;
int cCallback()
{
    [refToSelf someMethod:someArg]; // here objective-c method is calling in c.
}

@implementation SomeClass
- (id) init
{
     self = [super init];
     refToSelf = self;
}
- (void) someMethod:(int) someArg
{
}

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 992697

You don't even have to put your C code in a different file, you can simply just write C code in your .m file. The Objective-C compiler accepts C code.

If, at a later time, you would like to figure out how to separate your code into different files and use header files, you can do that too.

Upvotes: 3

Related Questions