具有整数对象参数的目标C performselector提供垃圾值

Sex*_*ast 2 objective-c ios performselector

我有一个带有以下接口的Rectangle对象:

@interface Rectangle : NSObject

  @property int height, width;
  -(void) setWidth:(int)w andHeight: (int)h;

@end
Run Code Online (Sandbox Code Playgroud)

我有一个实现,并有一个对象(比如说r)。当我打电话

[r setWidth: 5 andHeight: 6];
Run Code Online (Sandbox Code Playgroud)

通过验证时,我得到正确的结果[r height]。但是,当我使用performselector相同的方法时:

NSNumber *myNumber = [NSNumber numberWithInt:45];
[r performSelector:@selector(setWidth:andHeight:) withObject:myNumber
            withObject:myNumber];
Run Code Online (Sandbox Code Playgroud)

调用会[r height]打印一些垃圾值(看起来像某个地址),预期值为45。我在做什么错?

hid*_*eya 7

我有一个类似的问题,并使用解决了NSInvocation
像下面这样的代码应该可以工作,尽管它变得冗长:

SEL selector = NSSelectorFromString(@"setWidth:andHeight:");
NSMethodSignature *signature = [r methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
int x = 5;
int y = 6;
[invocation setArgument:&x atIndex:2]; // 0 and 1 are reserved
[invocation setArgument:&y atIndex:3];
[invocation invokeWithTarget:r];
Run Code Online (Sandbox Code Playgroud)