1
  1. I typedef a block like below

    typedef void(^cbGeneric)();
    
  2. I define a property using block in a class

    @property(nonatomic, strong)cbGeneric batchLoadStartedEvent;
    
  3. I need to bind a member method address of class to this property.

How to get pointer of member method of class and assign it to block property? thanks

2
  • Can you explain why? If you want the block to be invoked by a method, just write a method that invokes it. Commented Jun 3, 2017 at 20:19
  • for a event delegate instead of protocol of objective-c, because event of protocol need a class to implement it. It is not flexible than a method to hand event. Commented Jun 4, 2017 at 14:04

2 Answers 2

4

I need to bind a member method address of class to this property.

It is not clear what you are after here. Whether you are calling and instance or class method two implicit parameters need to be passed: a value for self, and a value of type SEL. You mention neither of these.

I suspect what you are trying to do is assign to the property a block value which will call some method, passing any parameters needed. In which case just assign a block value, something like:

obj.batchLoadStartedEvent = ^{ [someObject someMethod]; };

Where obj references the object whose property you wish to set and [someObject someMethod] is whatever method you wish to call, on whatever object or class, and includes any parameters you wish to pass.

HTH

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

2 Comments

currently, I just used this way to implement. But I think this way is not enough elegant.
This is the way (Objective-)C blocks look and behave. Blocks are similar to anonymous functions in C#. If you want the equivalent of C function pointers you need to store at least the function address itself (casting the IMP to the exact function type), a selector (SEL), and the object reference (the called function's self) - storing those 3 values is hardly more elegant than the above block expression. But then elegance is probably in the eye of the beholder!
2

You can get the member method address with class_getMethodImplementation() for example. But this does not help, because you cannot assign a method implementation (basically a function) to a block var. This is, because blocks are not simply function vars, but closures. And closures have a creational context, methods have an object context. That's not the same.

However, there are options to get,what you probably want to get:

Instead of having a block var, use a method implementation var of type IMP. If you execute the method, remember to pass a self pointer and a selector as expected by methods.

You can have a selector var, which stores the selector of the method to be executed. It's type is SEL. Then you can send a message using one method of the -performSelector: family.

You can assign a block to your current ivar, that does nothing else than sending a message with the method's selector. (Which "calls" the desired method.)

If you give more information about your situation, I can add a more detailed solution fitting to your problem.

8 Comments

Yes, I have try define IMP as a property, and try to assign value of selector to this property. But compiler could not recognize IMP as a property.
I want to give a method address to property as event handler like c# delegate. I can bind the other instance method to this method pointer to hand event.
I could answer to that comments, i. e. that IMP and SEL are different types. But you have a completely wrong approach. This is not C#. Please read an introduction to Objective-C's delegating mechanism.
Yes, I know event of objective-c is using protocol key word to implement event. But all events integrate to one instance of class. It is not flexible. IMAGINE: there are 3 events, if I need to split 3 events, I need to define 3 protocols and define 3 classes to implement them separately. If the callback can be bind to a method pointer directly, It is flexible, I can bind event and method point quickly instead of define protocol and class implemented.
You misunderstand the concept. Delegation is a kind of specialization and replaces subclassing for some reasons. There it makes no sense to have three different specialized instances. If you want to have "handlers" use the action target paradigma as described as option #2 in my answer. However, you shouldn't use implementations, because it destroys dynamic binding. Again: Read an introduction to Objective-C.
|

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.