问题描述
我尝试将CGRect传递给NSInvocation(setArgument:atIndex :).我用NSValue包装它,推到NSArry,然后从NSArray获取并使用NSValue(getValue :).调用(getValue :)方法会导致声明的索引NSInteger i的更改.谁能说为什么会这样?
I try to pass CGRect to NSInvocation (setArgument:atIndex:). I wrap it by NSValue, push to the NSArry,then get from NSArray and use NSValue (getValue:). Calling of (getValue:) method causes the changing of before declared index NSInteger i. Can anybody say why does this happen?
NSString className = @"UIButton";
Class cls = NSClassFromString(className);
cls pushButton5 = [[cls alloc] init];
CGRect rect =CGRectMake(20,220,280,30);
NSMethodSignature *msignature1;
NSInvocation *anInvocation1;
msignature1 = [pushButton5 methodSignatureForSelector:@selector(setFrame:)];
anInvocation1 = [NSInvocation invocationWithMethodSignature:msignature1];
[anInvocation1 setTarget:pushButton5];
[anInvocation1 setSelector:@selector(setFrame:)];
NSValue* rectValue = [NSValue valueWithCGRect:rect];
NSArray *params1;
params1= [NSArray arrayWithObjects:rectValue,nil];
id currentVal = [params1 objectAtIndex:0];
NSInteger i=2;
if ([currentVal isKindOfClass:[NSValue class]]) {
void *bufferForValue;
[currentVal getValue:&bufferForValue];
[anInvocation1 setArgument:&bufferForValue atIndex:i];
}else {
[anInvocation1 setArgument:¤tVal atIndex:i];
}
[anInvocation1 invoke];
当此(getValue:)
方法实现'i'的值时,其值从2变为1130102784,在(setArgument:atIndex:)
中,我有了一个SIGABRT,因为索引i超出范围.
When this(getValue:)
method implements value of 'i' changes from 2 to something like : 1130102784, and in (setArgument:atIndex:)
I have an SIGABRT because index i is out of bounds.
那为什么[NSValue getValue:(*void) buffer]
会更改其他变量?
So why does [NSValue getValue:(*void) buffer]
change other variables?
(P.S.我在函数中执行此操作,因此我简化了示例并直接初始化数组.如果我直接设置atIndex:2,它会完美.但是,由于我很难过,我简化了一点,我需要将我传递给atIndex:)
(P.S. I do it in function so I simplified an example and initialize array directly.And if I set directly atIndex:2, It works perfect.But as I've sad I simplified a little, and I need to pass i to atIndex:)
感谢汤姆·达林(特别是),塞尔吉奥问题得以解决.我删除了这个:
Thanks to Tom Dalling(especially) and sergio problem solved. I delete this:
void *bufferForValue;
[currentVal getValue:&bufferForValue];
[anInvocation1 setArgument:&bufferForValue atIndex:i];
并粘贴
NSUInteger bufferSize = 0;
NSGetSizeAndAlignment([currentVal objCType], &bufferSize, NULL);
void* buffer = malloc(bufferSize);
[currentVal getValue:buffer];
[anInvocation1 setArgument:buffer atIndex:i];
谢谢. Stackoverflow.com对聪明的人很有帮助.
Thank you. Stackoverflow.com really helpful site with smart people.
推荐答案
您不能将NSRect
放入void*
中.正确的代码是:
You can't fit an NSRect
into a void*
. The correct code would be:
NSRect buffer;
[currentVal getValue:&buffer];
或者,如果您不知道NSValue
对象的内容,则:
Or alternatively, if you don't know the contents of the NSValue
object:
NSUInteger bufferSize = 0;
NSGetSizeAndAlignment([currentVal objCType], &bufferSize, NULL);
void* buffer = malloc(bufferSize);
[currentVal getValue:buffer]; //notice the lack of '&'
//do something here
free(buffer);
i
的值正在更改,因为您的代码导致其他堆栈分配的变量溢出.
The value for i
is changing because your code causes an overflow into other stack-allocated variables.
这篇关于NSValue:getValue:奇怪的行为,为什么会这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!