当前方法的 NSInvocation 的 getArgument 总是返回 null [英] getArgument of NSInvocation of current method always returns null

查看:81
本文介绍了当前方法的 NSInvocation 的 getArgument 总是返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取我所在的当前函数的参数名称,以便我可以准备从文件系统加载该对象(如果当前实例中不存在该对象).(例如,如果 [foo dictTest] 不可用,我想将其先前保存的 plist 版本加载到该 ivar 中)

I want to get the name of the arguments of the current function I am in so that I can prepare loading that object from the filesystem if it's not present on the current instance. (for instance if [foo dictTest] is not available I want to load it's prior saved plist version into exactly that ivar)

我想通过提供我作为参数提供给当前函数的 ivar 名称来查找文件.

I want to find the file by providing the ivar name that I provided as an argument to the current function.

这是函数代码:

-(NSDictionary*)getCachedDictionary:(NSDictionary*)dict{

    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:_cmd]];
    NSString * firstArgument = nil;
    [invocation getArgument:&firstArgument atIndex:2];
    NSLog(@"Trying to get the objects ivar %@",firstArgument);

    // right now during testing return nil
    return nil;
   }

一旦代码到达 NSLog,我就会从 firstArgument 得到一个空值.

As soon as the code reaches the NSLog I am getting a null value from firstArgument.

这是为什么?是否有可能我必须等待我所在的当前方法的完整调用,或者实际上创建一个代理函数更好,该函数通过调用 setArgument 提供的 ivar 名称隐式调用我的类方法,以便我可以随意使用该参数字符串吗?

Why is that? Could it be possible that I would have to wait for the complete invocation of that current method I am in or is it actually better to create a proxy function that implicitly calls my class method via an invocation that eats the ivar name provided by setArgument so that I can use that argument string like I want?

非常感谢!

P.S.:在这个特定的例子中,我不想使用 KVC 来识别 ivar 并返回它.

P.S.: In this particular example I do not want to use KVC to identify the ivar and return it.

推荐答案

您误解了 NSInvocation API.+[NSInvocation invocationWithMethodSignature:] 创建一个新的 NSInvocation,它被键入以接受由方法签名定义的类型的参数.它返回对应于当前方法调用的NSInvocation.这很容易理解为什么:

You've misunderstood the NSInvocation API. +[NSInvocation invocationWithMethodSignature:] creates a new NSInvocation that is keyed to accept arguments of the types defined by the method signature. It does not return an NSInvocation that corresponds to the current method invocation. This is pretty easy to see why:

- (void)doBar:(id)bip {
  NSLog(@"hi there!")
}

- (void)doFoo {
  NSMethodSignature *sig = [self methodSignatureForSelector:@selector(doBar:)];
  NSInvocation *i = [NSInvocation invocationWithMethodSignature:sig];
}

当您在 doFoo 中为 doBar: 方法创建调用时,很明显可以看到参数必须为空,因为 doBar: 没有被执行,因此没有参数.将 @selector(doBar:) 更改为 _cmd 不会神奇地改变任何东西.

When you create the invocation in doFoo for the doBar: method, it's obvious to see that the arguments must be empty, because doBar: hasn't been executed, and thus there is no argument. Changing @selector(doBar:) to _cmd wouldn't magically change anything.

那么下一个问题:有没有办法获得当前方法调用的NSInvocation?从来没听说过.NSInvocation 是一个极其复杂的类,从当前方法构造一个将是一场噩梦.

So the next question: is there a way to get an NSInvocation for the current method invocation? Not that I know of. NSInvocation is an extremely complicated class, and constructing one from the current method would be a nightmare.

我强烈建议您找到一种不同的方法来做您想做的任何事情.

I strongly suggest finding a different approach to do whatever it is you want to do.

这篇关于当前方法的 NSInvocation 的 getArgument 总是返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆