当我将NSInvocation的返回值设置为NSString时,调用者接收到了一个NSCFString。
在我的情况下,我正在模拟从单元测试中包含的文件中提取捆绑路径:
[[[_bundlePartial stub] andDo:^(NSInvocation *invocation) {
NSString* resourceName = [invocation getArgumentAtIndexAsObject:2];
NSString* type = [invocation getArgumentAtIndexAsObject:3];
NSString* path = [[NSBundle bundleForClass:self.class] pathForResource:resourceName ofType:type];
if (!path)
{
path = [_bundleOriginal pathForResource:resourceName ofType:type];
}
[invocation setReturnValue:(void*)path];
}] pathForResource:OCMOCK_ANY ofType:OCMOCK_ANY];
我这样称呼它:
NSString* jsonPathInBundle = [[NSBundle mainBundle] pathForResource:self.fileName ofType:self.fileExtension];
不幸的是,我得到了一个NSCFString。这是有道理的,因为我的NSString是由NSCFString支持的,但当我失去桥接时,我不能再在对象上调用NSString实例方法。有没有一种方法可以将值作为NSString返回?
NSString* json = [NSString stringWithContentsOfFile:jsonPathInBundle encoding:NSUTF8StringEncoding error:error];
,因为stringWithContentsOfFile:encoding:error:不是NSCFString上的方法。 - Ben Flynn