这个问题催生了这个问题.使用可可中的结构列表并不简单.使用NSArray和编码/解码,或使用C类型数组并丢失NSArray的商品.结构应该是简单的,但是当需要列表时,人们倾向于构建一个类.
何时使用结构列表在可可中有意义?
我知道关于结构与类的问题已经有很多问题了,我读过用户认为它对每种语言都是相同的答案,但至少可可应该有自己的特定答案,如果只是因为KVC或绑定(如彼得在第一个问题上建议).
我尝试将CGRect传递给NSInvocation(setArgument:atIndex :).我用NSValue包装它,推送到NSArry,然后从NSArray获取并使用NSValue(getValue :).调用(getValue :)方法会导致更改前声明的索引NSInteger i.任何人都可以说为什么会这样?
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];
Run Code Online (Sandbox Code Playgroud)
当这个(getValue:)方法实现'i'的值从2更改为类似的东西:1130102784,并且(setArgument:atIndex:)我有一个SIGABRT因为索引i超出界限. …