【发布时间】:2014-03-27 21:59:55
【问题描述】:
我有一个数组,我正在迭代并寻找一个特定的标志。如果标志值为nil,我正在调用一个生成调用对象并返回调用结果的方法。
我的代码结构如下
for(NSString *key in [taxiPlanes allKeys])
{
Plane *currentPlane = [taxiPlanes objectForKey:key];
if(currentPlane.currentAction == nil)
{
NSString *selector = [[currentPlane planeTakeoffSequence] firstObject];
currentPlane.currentAction = selector;
// Calling for NSInvocation in [self ...]
NSArray *action = [NSArray arrayWithArray:[self operationFromTakeoffAction:currentPlane.currentAction AtPoint:currentPlane.position]];
NSLog(@"%@",action);
}
}
生成 NSInvocation 的方法
-(NSArray *) operationFromTakeoffAction:(NSString *) action AtPoint:(CGPoint) flightPoint
{
NSMethodSignature *methodSignature = [FlightOperations instanceMethodSignatureForSelector:NSSelectorFromString(action)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:fOps];
[invocation setSelector:NSSelectorFromString(action)];
[invocation setArgument:&flightPoint atIndex:2];
NSArray *resultSet = [NSArray alloc]init];
[invocation invoke];
[invocation getReturnValue:&resultSet];
return resultSet;
}
在 for 循环中,没有对 NSInvocation ([self ....]) 的方法调用,循环执行得很好,不会崩溃。但是当我介绍调用 NSInvocation 的方法时,我可以看到 NSLog in for 循环打印预期的 NSArray 结果,但它崩溃并显示错误消息 EXC_BAD_ACCESS。
即使 NSInvocation 返回正确的结果,我也无法弄清楚为什么它会失败。如果没有 NSInvocation,for 循环不会崩溃。
任何建议都会有所帮助。
谢谢
【问题讨论】:
-
您是否有更多关于控制台或调试器崩溃原因的信息?
-
控制台不显示其他信息。我得到的只是线程 1:EXC_BAD_ACCESS(code=EXC_i386_GPFLT)
-
设置 NSZombieEnabled。它会给你更多的信息。见stackoverflow.com/questions/2190227/…
-
试试看能不能得到这里描述的信息:stackoverflow.com/questions/13326550/…
-
打开僵尸,这就是我得到的“2014-02-25 16:25:42.152 MyPlane[24339:70b] *** -[__NSArrayI release]: message sent to deallocated instance 0x1127717f0” .看起来像是过早的释放。
标签: ios objective-c nsinvocation