gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:47:28 26 4
gpt4 key购买 nike

在 xcode 中,我可以使用图片中的按钮找到给定方法的调用者。

是否可以在运行时做?

类似于:

-(NSArray *)getCallersOfFoo {

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

}

-(void)foo {...}

enter image description here

最佳答案

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

代码有点“被盗”,但我不知道从哪里来的。

#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> %@", [self class], self, 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您可能会发现非常有用。

关于objective-c - Xcode/Objective-c - 如何以编程方式查找给定方法的调用者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31536639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com