繁体   English   中英

NSInvocation返回nil

[英]NSInvocation returning nil

有谁知道如何将NSInvocation的参数设置为nil 我正在尝试使用OCMock并且希望此期望返回nil。 问题是,调用该方法时,我需要做其他事情,如您在示例中所看到的,这就是为什么我不这样做andReturn:nil

我想做这个:

[[[myObject stub] andDo:^(NSInvocation *inv) {
   [inv setReturnValue:Nil];
   [inv invoke];
   [self notify:kXCTUnitWaitStatusSuccess]; //Code that I need to execute
}] myMethod:OCMOCK_ANY];

但是我得到一个错误:

 *** Terminating app due to uncaught exception 
 'NSInvalidArgumentException', reason: '-[NSInvocation setArgument:atIndex:]: 
 NULL address argument'

有人知道是否还有另一种方法可以将其设置为nil吗? 还是不可能?

首先,您的代码没有任何意义。 您应该在调用之前设置调用的参数,所调用的方法将读取您提供给它的参数,并设置返回值,然后可以在调用之后读取返回值。 对您而言,在调用返回值之前先对其进行设置是没有意义的,因为根据定义,“返回值”是调用后“返回”的事物,并且将在调用结束时进行设置,并覆盖您的所有内容设置之前。

为了解决您遇到的错误,对于所有getArgumentsetArgumentgetReturnValuesetReturnValue函数,您需要将指针传递到要读取或写入值的缓冲区。 这是因为参数和返回值可以是各种大小的各种C类型,因此无法直接传递它。 在这种情况下,似乎要读取/写入的值是一个对象指针,因此您需要创建一个对象指针类型的变量,设置为所需的值(如果要设置的话),然后将指向此变量的指针传递给getArgument / setArgument / getReturnValue / setReturnValue

@newacct指出了这一点,他是完全正确的。 您不能在实际调用之前设置调用的返回值。 为了使调用结果为“ nil ”,我所做的就是修改调用的参数,以便调用将要调用的代码生成nil 让我用一个例子解释一下:

- (void)viewDidLoad {
  NSString *arg = @"HelloWorld";

  NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(isHelloWorld:)]];
  [invocation setSelector:@selector(isHelloWorld:)];
  [invocation setTarget:self];
  [invocation setArgument:&arg atIndex:2];

  NSString *result1 = nil;
  [invocation invoke];
  [invocation getReturnValue:&result1];

  NSString *result2 = [self resultOfModifiedInvocation:invocation];

  NSLog(@"result 1: %@",result1);
  NSLog(@"result 2: %@",result2);
}

- (NSString *) resultOfModifiedInvocation:(NSInvocation *) invocation {
  NSString *aString = @"NoWorld";
  [invocation setArgument:&aString atIndex:2];

  [invocation invoke];
  [invocation getReturnValue:&aString];

  return aString;
}


- (NSString *) isHelloWorld:(NSString *) aString{
  if([aString isEqualToString:@"HelloWorld"])
    return aString;
  else
    return nil;
}

方法isHelloWorld:应该返回@"HelloWorld"因为我们在调用中发送的内容是正确的。 实际上,由于参数尚未修改,因此在您第一次调用调用时实际上会发生这种情况。 另一方面,通过调用resultOfModifiedInvocation:调用的参数将被修改,然后调用的结果将有所不同。

这是示例的输出:

  > result 1: HelloWorld
  > result 2: (null)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM