mikeash/MAObjCRuntime

Methods with primitive arguments

mbrandonw opened this issue · 10 comments

I can't seem to invoke methods that have arguments which are not 4-bytes (i.e. BOOL, CGSize, etc...). It works fine with NSInteger, CGFloat and objects. Is this a known limitation?

It always aborts at the line checking if(inSize != sigSize) in RTMethod.m

Works fine for me. Can you show your code?

Sorry, I just realized I'm doing something stupid. I have a setter selector, I convert it into the corresponding getter, and then try to invoke it to get it's return value:

void *value = NULL;
[self rt_returnValue:&value sendSelector:getterSelector];

Since I don't know the return type I just did a void pointer, so of course 4 bytes. And then later I do:

[target rt_returnValue:NULL sendSelector:method.selector, RTARG(value)];

Is it possible to do what I want?

I'm not really sure I follow what you're trying to do. Can you post some more complete code that illustrates the problem?

The exact snippet is around line 120: https://github.com/mbrandonw/OPUIKit/blob/master/OPUIKit/Source/OPStyle.m

Basically, an OPStyle instance has a bunch of properties that ultimately get transferred to a target. To do that I loop through the methods in [OPStyleProtocol](https://github.com/mbrandonw/OPUIKit/blob/master/OPUIKit/Source/OPStyleProtocol.h, for each setter I find the corresponding getter, and then do the little snippet I posted above to transfer the property.

Does that make any more sense?

Yes, I think I see. Seems to me like this could be done more cleanly by using KVC. Use valueForKey: to get the value, then setValue:forKey: to set it. All problems of argument size, primitives, etc. are handled for you by the KVC internals.

I should mention for completeness that there's no good way to do this with MAObjCRuntime directly. The method invocation stuff there is only intended for cases where you know the types in advance. You could do it directly with NSInvocation, but that would be harder than just using KVC.

Wow, that is much cleaner. So, even though we cannot do something like [target setValue:CGRectMake(0,0,100,100) forKey:@"frame"] directly, if we retrieve an value from an object we can set it another object, even if it is a primitive?

Right. Primitives get wrapped in NSValue objects, and automatically unwrapped on the other side.

Terrific, thanks so much for the info.

Glad I could help!