加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Xcode / Objective-c – 如何以编程方式查找给定方法的调用者

发布时间:2020-12-14 17:24:06 所属栏目:百科 来源:网络整理
导读:在 xcode中,我能够使用图片中的按钮找到给定方法的调用者. 是否可以在运行时进行? 就像是: -(NSArray *)getCallersOfFoo { // is it possible to find the callers of the method foo?}-(void)foo {...} 解决方法 不完全是答案,但它可能有所帮助.此方法将
在 xcode中,我能够使用图片中的按钮找到给定方法的调用者.

是否可以在运行时进行?

就像是:

-(NSArray *)getCallersOfFoo {

  // is it possible to find the callers of the method foo?

}

-(void)foo {...}

解决方法

不完全是答案,但它可能有所帮助.此方法将为您提供打印输出堆栈或调用区域中的调用方.您可以修改它们当然可以随意使用这些值.

代码有点“被盗”,但我没有提到哪里.

#define SHOW_STACK NSLog(@"%@",[NSThread callStackSymbols])

#define SHOW_CALLER 
do {                
NSArray *syms = [NSThread  callStackSymbols]; 
if ([syms count] > 1) { 
    NSLog(@"<%@ %p> %@ - caller: %@ ",[self class],self,NSStringFromSelector(_cmd),[syms objectAtIndex:1]); 
} else { 
    NSLog(@"<%@ %p> %@",NSStringFromSelector(_cmd)); 
} 
} while(0)

编辑:你可能想要这样的东西:

NSString *caller = nil;
NSArray *syms = [NSThread  callStackSymbols];

if (syms.count > 1)
{
    caller = syms[1];
}

if (caller.length)
{
    NSLog(@"%s called by %@",__PRETTY_FUNCTION__,caller);
}

你可能会发现another Q&A here on SO非常有用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读