3

I'm trying to figure out how to call a c function from an obj-c file. I've seen some good examples of how to do the opposite.

In the example below I have an objective c file. It contains a c function named setup. I want to be able to create an instance of my obj-c file in the regular way and then call the setup function.

Header

#import <Foundation/Foundation.h>

void setup(int,float);

@interface Test : NSObject {
}

@end

Source

#import "Test.h"
#include <stdio.h>
#include <stdlib.h>

void setup(int val1,float val2)
{
    //do something with values  
}

@implementation Test

@end

View did load

Test *test =[Test alloc]init]

//this does not work
test.setup(6,1.4);
1
  • What's wrong with using an init function to set up your object? Commented Jun 13, 2011 at 10:53

1 Answer 1

11

Just call setup(). As declared, is in no way tied to an object - it's just a regular C function.

Sign up to request clarification or add additional context in comments.

2 Comments

please excuse my ignorance but lets say I had a struct in test called myStruct how could I ensure that calling setup would operate on that structs values? Would I have to pass an actual pointer to that struct to the setup function or something?
Yes, you would. C is still C in the land of objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.