mikeash / MAObjCRuntime

ObjC wrapper for ObjC runtime API
Other
1.53k stars 191 forks source link

Methods with primitive arguments #9

Closed mbrandonw closed 12 years ago

mbrandonw commented 12 years ago

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

mikeash commented 12 years ago

Works fine for me. Can you show your code?

mbrandonw commented 12 years ago

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?

mikeash commented 12 years ago

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

mbrandonw commented 12 years ago

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?

mikeash commented 12 years ago

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.

mikeash commented 12 years ago

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.

mbrandonw commented 12 years ago

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?

mikeash commented 12 years ago

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

mbrandonw commented 12 years ago

Terrific, thanks so much for the info.

mikeash commented 12 years ago

Glad I could help!