我想获取我所在的当前函数的参数名称,以便如果当前实例上不存在该对象,我可以准备从文件系统加载该对象。(例如,如果 [foo dictTest] 不可用,我想将它之前保存的 plist 版本加载到该 ivar 中)
我想通过提供我作为参数提供给当前函数的 ivar 名称来查找文件。
这是功能代码:
-(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 获得一个空值。
这是为什么?有没有可能我必须等待我所在的当前方法的完整调用,或者创建一个代理函数实际上更好,该函数通过调用 setArgument 提供的 ivar 名称来隐式调用我的类方法,这样我可以像我想要的那样使用那个参数字符串吗?
提前非常感谢!
PS:在这个特定的示例中,我不想使用 KVC 来识别 ivar 并返回它。