3

I have an objective c class that has a NSString object with property like that :

@property (nonatomic,retain) NSString *data;

now, from a c function , i want to do : data=@"abc"; , but the c function does not see data .

How would i access data from a pure c function in that same class ? I have tried with creating an object of that class within the c function but it crashes.

EDIT :

I have also tried to call obj c function with argument , i can call the function but without the argument : (in the C function )

     NSString *ran=@"";
    [refToSelf postNotification:ran];

do that without passing ran is working. how would i pass ran ??

5
  • C function is in the same class or other file? Commented Jan 26, 2013 at 9:39
  • Although the question was about C, you might want to investigate using C++, as in stackoverflow.com/questions/3807857/… Commented Jan 26, 2013 at 9:41
  • its in the same class as i wrote. i know there is simple way but couldnt find it . Commented Jan 26, 2013 at 9:47
  • what error are you getting trying to call postNotification? is your C function defined inside of a .m file? Commented Jan 26, 2013 at 11:29
  • 1
    You can't set the property with data = @"abc"; inside the class, either. You would be setting the property's instance variable directly, if it even has one by that name (which depends on several things). The way to set the property inside the class is either myObject.data or [myObject setData:] (which are equivalent to each other), which are also the ways you'd set the object's property from another class and how you'd set it from a C function. The key thing is that this is a property of some object, and you must have the object in hand in order to set a property of it. Commented Jan 26, 2013 at 19:53

1 Answer 1

15

If you have a C function in an Objective-C file, then the requirement is simple; you need a reference to the object you want to mess with.

void func(MyClass* anObj) {
    [anObj setData: @"foo"];
    anObj.data = @"foo"; // same as the line above
}

@interface MyClass:NSObject
@property(nonatomic,copy) NSString *data;
@implementation MyClass
- (void) callCFunc
{
     func(self);
     // the "data" property will now be set to @"foo"
}
@end

No more, no less. There is absolutely nothing different about calling a method in a C function than there is about calling a method of some other instance or class from a method. In fact, methods are nothing more than C functions with a default self and _cmd arguments.

It isn't clear what you are asking with your edit. If you "can't pass ran" then that implies that you are calling a method with no arguments:

- (void)postNotification;

If you want to pass ran, then you need a method that can consume the argument:

- (void)postNotification:(NSString*)data;
Sign up to request clarification or add additional context in comments.

3 Comments

Mmm... vindictive downvoting without reason? Nice!
What if the C function is invoked as a callback function by another C function? In that case I can't add as parameter the reference to my objective-c object. What can I do ?
If there isn't a context pointer that flows through the C API, then you are hosed anyway. I.e. think of it in terms of pure C; how would you pass the state that you need to reconnect to your app's state through said API? Whatever mechanism is used there can be used in Objective-C.

Your Answer

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