I would like to derive a MyInvocation from NSInvocation, this MyInvocation with member variables.
But only through examples NSInvocation + (NSInvocation *) invocationWithMethodSignature: (NSMethodSignature *) signature obtained.
In other words, NSInvocation's init is invalid. By overriding the init function can not be instantiated MyInvocation.
So how do I instantiate MyInvocation it?
Reply:
Subclass also by class initialization method, namely rewriting invocationWithMethodSignature
Also this class does not seem necessary to derive it?
Reply:
How to rewrite?
I use a derived class to achieve a similar function adepter pattern.
Reply:
Init with rewriting the same, but now is the rewrite class method:
+ (NSInvocation *) invocationWithMethodSignature: (NSMethodSignature *) signature
{
......
return [super invocationWithMethodSignature: signature];
}
Reply:
@ Interface Adepter: NSInvocation {
int paramIndex;
}
@ End
+ (Adepter *) invocationWithMethodSignature: (NSMethodSignature *) signature
{
......
return [super invocationWithMethodSignature: signature];
}
So? In addition, I need to set the value paramIndex instance before return, how to do it?
Reply:
If you need paramIndex value passed in as a parameter, you need to define your own constructor
For example:
+ (Adepter *) invocationWithMethodSignature: (NSMethodSignature *) signature index: (int) index
{
paramIndex = index;
return [super invocationWithMethodSignature: signature];
}
Reply:
Unfortunately, this method is unable to access paramIndex class this entity variable. Have you tried to compile yet?
Reply:
Oh, right, forgot about it children, it seems that the derived class method does not work.
So go back to the original question, this class does not derive the necessary
You want to find the corresponding method by index entirely by NSInvocation's setTarget, setSelector to complete
NSInvocation itself is already an adapter of
Reply:
So it seems like as long as it does not provide the init method of the class is no way to derive it.
NSInvocation very primitive, only setArgument: atIndex:, but I want is a can continue without additional parameters to care index of adpter, this NSinvocation not.
Reply:
I think you do not have to consider the derived class, isa not there has-a
With a class contains two instance variables NSInvocation, int no good?
Reply:
I am now doing. Then use the message forwarding to simulate derived. Send this paste is to see if it is in addition to this method there are other more concise way.
Reply:
Of course, you can also use objc runtime simulation of what inheritance, but now with a more ugly than me.
No comments:
Post a Comment