gpt4 book ai didi

objective-c - 获取 Objective-C 类或实例的所有方法

转载 作者:IT老高 更新时间:2023-10-28 11:50:11 36 4
gpt4 key购买 nike

在 Objective-C 中,我可以测试给定的类或实例是否响应某些选择器。但是如何查询一个类或实例的所有方法或类的属性(例如所有方法或属性的列表)?

最佳答案

除了 Buzzy 的回答,出于调试目的,您可以使用 -[NSObject _methodDescription] 私有(private)方法。

在 lldb 中:

(lldb) po [[UIApplication sharedApplication] _methodDescription]

或在代码中:

@interface NSObject (Private)
- (NSString*)_methodDescription;
@end

// Somewhere in the code:
NSLog(@"%@", [objectToInspect performSelector:@selector(_methodDescription)]);

输出将如下所示:

<__NSArrayM: 0x7f9 ddc4359a0>:
in __NSArrayM:
Class Methods:
+ (BOOL) automaticallyNotifiesObserversForKey:(id)arg1; (0x11503b510)
+ (id) allocWithZone:(_NSZone*)arg1; (0x11503b520)
+ (id) __new:::::(const id*)arg1; (0x114f0d700)
Instance Methods:
- (void) removeLastObject; (0x114f669a0)
- (void) dealloc; (0x114f2a8f0)
- (void) finalize; (0x11503b2c0)
- (id) copyWithZone:(_NSZone*)arg1; (0x114f35500)
- (unsigned long) count; (0x114f0d920)
- (id) objectAtIndex:(unsigned long)arg1; (0x114f2a730)
- (void) getObjects:(id*)arg1 range:(_NSRange)arg2; (0x114f35650)
- (void) addObject:(id)arg1; (0x114f0d8e0)
- (void) setObject:(id)arg1 atIndex:(unsigned long)arg2; (0x114f99680)
- (void) insertObject:(id)arg1 atIndex:(unsigned long)arg2; (0x114f0d940)
- (void) exchangeObjectAtIndex:(unsigned long)arg1 withObjectAtIndex:(unsigned long)arg2; (0x114f8bf80)
......
in NSMutableArray:
Class Methods:
+ (id) copyNonRetainingArray; (0x11ee20178)
+ (id) nonRetainingArray; (0x11ee201e8)
+ (id) nonRetainingArray; (0x120475026)
+ (id) arrayWithCapacity:(unsigned long)arg1; (0x114f74280)
......

关于objective-c - 获取 Objective-C 类或实例的所有方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2094702/

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